Understanding Linux Commands: dos2unix and unix2dos

Prevent problems when moving Windows files to Linux

Close up hands typing on a laptop
Eternity in an Instant / Getty Images

Linux is the best-known of the open source operating systems. Occasionally, you may need to move files between Windows and Linux systems. In Windows/DOS files, a line break is indicated by two characters, the carriage return (CR) and line feed (LF). The end of line indicator in Linux/Unix files is indicated by only one character, the carriage return (CR). This difference can cause troublesome hidden characters when you move files between systems.

However, avoiding the hidden characters is relatively painless.

  • The dos2unix command converts plain text files in Windows to Linux format.
  • The unix2dos command converts plain text files in Linux to Windows format.

These commands affect the line ending of text files. They do not alter binary files (unless you force a conversion) or nonregular files such as FIFOs and directories.

Syntax

dos2unix [options] [FILE...] [-n INFILE OUTFILE ...]
unix2dos [options] [FILE...] [-n INFILE OUTFILE ...]

Options

The following options are a partial list of the most frequently used options available for dos2unix and unix2dos:

-h, --help  Display help and exit.

-k, --keepdate Keep the date stamp of output file same as input file.

-l, --newline – Add an additional newline

-q, --quiet  Quiet mode. Suppress all warning and messages.

-V, --version   Display version information and exit.

-f, --force – Force conversion of binary files

-s, --safe – Skip binary files (default)

-F, --follow-symlink – Follow symbolic links and convert the targets

-S, --skip-symlink – Leave symbolic links and targets unchanged (default)