DOS and UNIX end-of-line characters

The UNIX end-of-line character is a line feed/newline character (\n). The DOS/Windows end-of-line character is a carriage return, followed by a line feed/newline (\r\n).

Most editors can automatically convert these to the appropriate format, but others, such as Notepad in Windows, cannot.

To convert a UNIX file to DOS using sed (GNU sed 3.02.80 or later):
$ sed 's/$/\r/' UNIX_file > DOS_file

To convert a DOS file to UNIX file, use tr to remove the carriage return:
$ tr -d '\r' < DOS_file > UNIX_file

To accomplish the same thing using sed:
$ sed 's/^M//' DOS_file > UNIX_file

Note: To generate the ^M above, press Ctrl-V, then Ctrl-M.

FTP's ASCII transfer mode will automatically do the end-of-line conversion between DOS and UNIX systems.

Also, the commands dos2unix and unix2dos may be used to perform the conversion, if present on your system.

More information:
Newline (Wikipedia)

Back to
Last modified: 2006/12/07