ftp appends ^M to .Z file - bash

I am doing ftp in binary mode and downloading some compressed_file.Z but I get compressed_file.Z^M and when I am using dos2unix it creates a new file with ^M removed but still when I try to uncompress it, I get error saying Corrupt Input or its not recognized as tar file etc. If I paste it in my windows machine using filezila, I am able to open it with 7Z. I have also used sed 's/^M$//' compressed_file.dmp.Z^M>compressed_file.dmp.Z but still it is corrupt file. Has someone faced similar issue?

It's likely that it's only your filenames that contains ^M at the end. You could rename them with:
for A in *.Z$'\x0d'; do
mv -i "$A" "${A%?}" # Remove -i to skip confirmation
done

Removing specification of any kind of mode say ASCII or BINARY etc solved my problem.

Related

Wrong filenames after unzipping on ubuntu

Problem
I have a zip-file that I would like to unzip on Ubuntu with the correct filenames (they contain æ,ø,å).
What I have tried:
1. Unrar in Windows 10 - WORKS!
Everything works as expected and filenames are correct.
2. Unzip in Ubuntu
unzip file.zip
The characters æ,ø and å are missing from the filenames, where 'æ' has been replaces with 'C'.
I attempt to detect the encoding of the zip-file, but it doesn't seem to tell me anything.
file file.zip
3. Unzip with encoding in Ubuntu
I attempt to unpack the file using various encodings that are often used for æ,ø,å-containing texts.
unzip -O UTF-8 file.zip
unzip -O ISO-8859-1 file.zip
unzip -O windows-1257 file.zip
None work...
4. Unzip using 7zip in Ubuntu
It is suggested that 7zip may fix the problem, but no..
7z x file.zip
5. Unzip using 7zip and danish language setting in Ubuntu
It is suggested that I change the ubuntu language settings and then try again.
saveLang=$LANG
export LANG=da_DK
7z x file.zip
export LANG=$saveLang
This also does not work.
6. Unzip using Python3 in Ubuntu - WORKS!
The unzip works correctly if I use Python3 for the purpose, but there must be an easier way?
import zipfile
with zipfile.ZipFile('file.zip', "r") as z:
z.extractall("/home/xxxx/")
7. Next step
I am considering finding a list of "ALL" encodings, and then just extracting the filenames and going through them manually. Something along the line of this...
while read p; do
echo "$p"
unzip -j -O $p file.zip
done <encodings.txt
Conclusion
Windows and Python3 seems to have some MAGIC under the hood that I cannot replicate. Do you guys have any suggestions to what this "MAGIC" is?
How do I identify the encoding of the filenames of a zip-file?
Where can I get a list of all encodings for step 7.
Is there any easy way to solve this problem without having to write e.g. a python script?
The key piece of information you provided was that unrar on windows was able to create the filenames correctly. So unless unrar is doing some encoding detection under the hood, that meant that there is a good chance that the encoding used in the zip files matches the default codepage used on your Windows setup.
Using chcp on Windows you see that your codepage is
Active code page: 850
It's then a simple matter of telling unzip that the encoding used in the zip file is CP850
unzip -O CP850 file.zip

Removing a .docx file starting with ~$ via the terminal

After playing around in the terminal I found that my desktop directory had a file named "~$Löp.docx" that wasn't visible outside the terminal. The file seems to be some kind of crash log for a (now deleted) word file called "Löp". I would like to remove the file and have thus far failed to enter a rm command that works.
When trying to echo the filename as
echo "~$Löp.docx"
I get the output
~?p.docx
How can I retain the correct name for a rm statement?
Just Quote it, here's an example:
rm '~$some file name.docx'

macOS How to read Pages/Numbers file in command line?

macOS How to read Pages/Numbers file in command line ?
I have userd 'cat'
cat /Users/administrator/Downloads/test.pages
but get:
????(??#?-QEQEQEQEQEQEQEQE?kYe??Ȍ?"8?,?9A?i;??1]?????=.SE[????Sqs-?,?iY??3]]QEQEQEQEQEQEQEQE??????(??(??(??(??(??(??(??(??(??(??????(??(??(??(??
addtional
I can use cat to watch .md file
Pages/Numbers files are .zip archives of binary files, not text files. In order to read them on the command line, you'll need a special tool, though I don't believe one exists at the moment

How to merge multiple CSV files into one using terminal on MAC OS?

I have the directory with 20 csv files. They all have same headers. I want to merge these csv files into one file. When I'm working on windows, I just open cmd, go to the right directory and use this command:
copy *.csv combined-files.csv
It does the job on Windows. I can't run this command on mac's terminal as command doesn't exist on Mac. How do I rewrite this Windows command so it does the same job on Mac OS?
Easiest I can think of, which would be similar to the Windows version is:
cat *.csv > combined-files.csv
Note that the headers would be repeated throughout the combined-files.csv if they are contained in all the .csv-files. Just like the solution you posted for Windows.
To concatenate all files into a single file, you can use the following awk command to consolidate the content.
This will ignore the header content though
awk 'FNR > 1' *.csv > consolidated.csv
The awk command assumes that all the files are in one directory.
If all of your files are in different directories, you can use the below command to bring it together into one directory
find . -name \*.csv -exec cp {} newdir/ \;

How to stop new line conversion when zipping a Windows text file on a Unix machine

I want to zip a windows .cmd file on an OSX server, using the zip command line tool.
templateName="Windows_Project_Template"
zip -r -T -y -9 "${templateName}.zip" $templateName
When the file is unzipped on a windows machine all the new line carriage returns are converted and so the text file comes out without any new line formatting on a windows machine. How can I work around this?
Thanks
While not a perfect solution (I can't find an option to handle everything as binary), you can force the \r\n with the --to-crlf option:
-l
--to-crlf
Translate the Unix end-of-line character LF into the MSDOS convention CR LF. This option should not be used on binary files. This
option can be used on Unix if the zip file is intended for PKUNZIP under MSDOS. If the input files already contain CR LF, this
option adds an extra CR. This is to ensure that unzip -a on Unix will get back an exact copy of the original file, to undo the
effect of zip -l. See -ll for how binary files are handled.
Be careful, if the file already contains \r\n you will get \r\r\n.

Resources