LinuxCommandLibrary

dos2unix

Convert DOS line endings to Unix

TLDR

Change the line endings of a file

$ dos2unix [path/to/file]
copy

Create a copy with Unix-style line endings
$ dos2unix [[-n|--newfile]] [path/to/file] [path/to/new_file]
copy

Display file information
$ dos2unix [[-i|--info]] [path/to/file]
copy

Keep/add/remove Byte Order Mark
$ dos2unix --[keep-bom|add-bom|remove-bom] [path/to/file]
copy

SYNOPSIS

dos2unix [options] [file ...]

PARAMETERS

-ascii
    Convert only line breaks. This is the default conversion mode.

-iso
    Convert according to ISO Latin-1.

-1252
    Convert according to Windows code page 1252.

-437
    Convert according to DOS code page 437.

-m
    Preserve last modification timestamp of the input file.

-n infile outfile
    Convert 'infile' and write to 'outfile'. The input file is not modified.

-o file
    Overwrite original file (default). Equivalent to running dos2unix without the -n option.

-q
    Quiet mode. Suppress all warnings and messages.

-v
    Verbose mode. Shows conversion statistics.

-f
    Force conversion, even if the file seems to be in Unix format already.

-D
    Debug mode.

-h
    Display help text and exit.

-V
    Display version information and exit.

DESCRIPTION

The dos2unix command converts text files from the DOS/Windows format to the Unix format. DOS/Windows uses carriage return and line feed (CRLF) to mark the end of a line, while Unix uses only a line feed (LF). This difference can cause problems when working with text files created on one operating system on another. dos2unix removes the carriage return characters from the end of each line, thus converting the file to the Unix format. It also handles other text file format differences like encoding (e.g., UTF-16 to UTF-8). The command attempts to convert the input file in place, overwriting the original file. It can also be used with input redirection for non-in-place conversion. Batch processing of multiple files is possible. This ensures compatibility and proper display of text files across different operating systems.
dos2unix is essential for cross-platform development and administration.

CAVEATS

If no input files are given, dos2unix reads from standard input and writes to standard output. When converting in place (default behavior), make a backup of important files before running dos2unix, as the original file will be overwritten.

EXIT STATUS

The dos2unix command returns an exit status of 0 on success, and a non-zero value on failure.

ENCODING CONVERSION

Besides converting line endings, dos2unix can also convert between different character encodings, such as UTF-16 and UTF-8. It is important to specify the correct encoding using the appropriate options if the file is not encoded in ASCII.

HISTORY

The dos2unix command has been around for a long time and has been reimplemented to be more compatible across systems. It became necessary due to the rise of DOS and Windows as dominant operating systems and the need to exchange text files between these systems and Unix-like systems. Development has continued to expand the command's capabilities, including support for various character encodings and more robust handling of different file types. Its usage is widespread in cross-platform development environments and system administration.

SEE ALSO

unix2dos(1), sed(1), tr(1)

Copied to clipboard