Convert DOS format text files to Unix format
dos2unix command is used to convert text files in DOS format to UNIX format (DOS/MAC to UNIX text file format converter). Text files under DOS use \r\n
as the line break mark, which is expressed in hexadecimal as 0D 0A. Text files under Unix use \n as the line break mark, which is expressed as 0A in hexadecimal. When a text file in DOS format is opened under Linux with a lower version of vi, ^M
will be displayed at the end of the line, and many commands cannot handle files in this format well, if it is a shell script. Text files in Unix format will be displayed together when opened with Notepad under Windows. Therefore, there is a need to convert files in two formats to each other. The corresponding command to convert UNIX format text files into DOS format is the unix2dos command.
dos2unix [-hkqV] [-c convmode] [-o file ...] [-n infile outfile ...]
-k: Keep the date of the output file unchanged
-q: Quiet mode, no warning message is prompted.
-V: View version
-c: Conversion mode, the modes are: ASCII, 7bit, ISO, Mac, the default is: ASCII.
-o: write to source file
-n: write to new file
Parameters: Need to be converted to file.
The simplest usage is for dos2unix to directly follow the file name:
dos2unix file
If converting multiple files at once, put the file names directly after dos2unix. (Note: You can also add the -o
parameter or not, the effect is the same)
dos2unix file1 file2 file3
dos2unix -o file1 file2 file3
During the above conversion, the original file will be modified directly. If you want to save the conversion result in another file without changing the source file, you can use the -n
parameter.
dos2unix oldfile newfile
If you want to keep the file timestamp unchanged, add the -k
parameter. Therefore, the above commands can be added with the -k
parameter to maintain file timestamps.
dos2unix -k file
dos2unix -k file1 file2 file3
dos2unix -k -o file1 file2 file3
dos2unix -k -n oldfile newfile
Convert all files in the current directory
find -type f | xargs dos2unix