Understanding Linux Commands: dos2unix and unix2dos

Prevent problems when moving Windows files to Linux

Hands of person typing on laptop

Dmitry Ageev / 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 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.

Using dos2unix

It's fairly simple to work with dos2unix. It only really requires an input file to do what you need, and most of the additional options are fairly situation specific, so you won't encounter them in more basic situations. For example, you could convert a DOS file with a basic command, like:

dos2unix text-file.txt

Your DOS file will instantly be converted to Unix encoding and will be ready to use.

If you'd prefer to keep the original and make a Unix encoded copy, you an accomplish that simply enough too.

dos2unix -n text-dos.txt text-unix.txt

You'll now have two files. One will be the original DOS one and the other will be the new Unix copy.

Linux dos2unix command

The same thing works in reverse with unix2dos, which converts your Unix encoded text files to DOS. This is perfect if you're exporting something from your Linux machine to a Windows one. The command is nearly identical.

unix2dos text-file.txt

Just like before, you can create a DOS encoded copy from your Unix text file.

unix2dos -n text-unix.txt text-dos.txt

Your Unix original will be preserved, but you'll also have a copy to send to Windows.

Linux unix2dos command

For more information on what dos2unix can do, check out the manual info below.

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).