Linuxstar
Published on

dos2unix command-Convert DOS format text file to UNIX format

Jonas

Jonas

2 min read

The dos2unix command is used to convert DOS format text files into UNIX format (DOS/MAC to UNIX text file format converter). The text file under DOS uses \r\n as the line break mark, expressed in hexadecimal is 0D 0A. The text file under Unix uses \n as the line break mark, which is 0A in hexadecimal.

DOS format text files under Linux, when opened with a lower version of vi, the end of the line will display ^M, and many commands cannot handle this format file well. If it is a shell script, the Unix formatted text file will be displayed together when opened with Notepad under Windows. Therefore, there is a need for mutual conversion of two format files, and the corresponding one that converts UNIX format text files into DOS format is the unix2dos command.

Syntax: dos2unix [parameter] [file]

Command parameters
-kKeep the date of the output file unchanged
-qQuiet mode, no warning message
-VView version
-cConversion mode: ASCII 7bit ISO Mac
-oWrite to source file
-nWrite to new file
Example

The simplest usage is dos2unix to directly follow the file name:

[root@linuxstar ~]# dos2unix file

If you convert multiple files at once, put these file names directly after dos2unix. (Note: You can also add -o parameter or not, the effect is the same)

[root@linuxstar ~]# dos2unix file1 file2 file3
[root@linuxstar ~]# dos2unix -o file1 file2 file3

During the above conversion, you will directly modify the original file. If you want to save the conversion result in another file, but the source file remains unchanged, you can use the -n parameter.

[root@linuxstar ~]# dos2unix -n oldfile newfile

If you want to keep the file timestamp unchanged, add the -k parameter. So you can add the -k parameter to the above commands to keep the file timestamp.

[root@linuxstar ~]# dos2unix -k file
[root@linuxstar ~]# dos2unix -k file1 file2 file3
[root@linuxstar ~]# dos2unix -k -o file1 file2 file3
[root@linuxstar ~]# dos2unix -k -n oldfile newfile