How to convert file between formats

Converting files in GNU/Linux(R)

Count and display number of lines in a text file.

Take the following command as an example.

wc -l my_results_table.txt

Discover metadata from a picture file.

Use as in the following command of "imagemagick" software to display the metadata from an image (picture) file:

identify -verbose photo.JPG

Convert End-of-line characters from old Mac style (0x0D) to Linux style (0x0A).

Take the following command as an example.

tr '\r' '\n' < ./foo.text > ./foo.linux.text

Convert a raster image into "BMP" format with optional color reduction

Use a command like the following to convert a file into a monochrome "BMP" file:

convert EXMPL001.PNG -monochrome -define bmp:format=bmp2 TEST1.BMP

Convert character encoding in text files.

Use "iconv" to convert text files between different character encodings. Character encodings are not always 100% compatible with each other. In case of incompatibility a way to mark problematic characters must be provided to the tool.

Convert a file from UTF-8 to Windows-1251 (cyrillic for Windows) encoding as in the following.

iconv -f UTF-8 -t WINDOWS-1251 -o output.txt input.txt

Carefully convert a file from UTF-8 to Windows-1251 (cyrillic for Windows) encoding as in the following. "--byte-subst" relates to the invalid character bytes in the input files. "--unicode-subst" relates to the missing characters in the output encoding.

iconv -f UTF-8 -t WINDOWS-1251 --byte-subst="<0x%x>" --unicode-subst="" -o output.txt input.txt

Convert a file from UTF-8 to CP866 (cyrillic for MS-DOS) encoding as in the following.

iconv -f UTF-8 -t CP866 -o output.txt input.txt

Convert a file from UTF-8 to KOI8-R (cyrillic for MS-DOS) encoding as in the following.

iconv -f UTF-8 -t KOI8-R -o output.txt input.txt

Convert line ending encoding between Unix and DOS formats

Use the following utilities: "dos2unix", "unix2dos".

Convert from DOS format to Unix format as in the following.

dos2unix -f -n INFILE.txt OUTFILE.txt

Convert from DOS format to Unix format as in the following.

unix2dos -f -n INFILE.txt OUTFILE.txt

Merge several "PDF" files into one.

Take the following command as an example.

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile=merged.PDF input1.PDF input2.PDF

Trademark notices

Linux(R) is registered trademark of Linus Torvalds in the United States and other countries.


Copyright (c) 2022 Leonid Dorogin