SoFunction
Updated on 2025-03-10

Detailed explanation of the Linux command dos2unix command example (convert DOS format text file to Unix format)

illustrate

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 flag, which is represented in hexadecimal, which is 0D0A. The text file under Unix uses \n as the line break flag, which is represented as 0A in hexadecimal. DOS format text files are under Linux. When opened with lower versions of vi, ^M will be displayed at the end of the line. Many commands cannot handle files in this format well. If it is a shell script, Unix-format text files will be displayed together when opened with Notepad under Windows. Therefore, the need to convert the two format files to each other is generated. The corresponding unix2dos command converts UNIX format text files into DOS format.

grammar

 dos2unix [-hkqV] [-c convmode] [-o file ...] [-n infile outfile ...]

Options

-k:Keep the date of the output file unchanged
-q:Quiet mode,No warning messages are prompted。
-V:View version
-c:Conversion mode,The mode has:ASCII, 7bit, ISO, Mac, The default is:ASCII。
-o:Write to source file
-n:Write to a new file

parameter

Parameters: Need to convert to file.

Example

The easiest way to use it is to directly keep up with the file name:

dos2unix file

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

dos2unix file1 file2 file3
dos2unix -o file1 file2 file3

When converting above, it will directly modify it on the original file. If you want to save the conversion result in another file, but the source file remains unchanged,
Then the -n parameter can be used.

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 the file
Timestamped.

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

This is the article about the detailed explanation of the Linux command dos2unix command example (converting DOS format text files to Unix format). For more related Linux dos2unix command content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!