How to convert line breaks in a text file between the Windows and Unix/Linux formats?

Solution 1:

You're probably looking for dos2unix, unix2dos, todos or fromdos depending on your distribution. Ubuntu/Debian package todos/fromdos as part of the tofrodos package from memory.

Solution 2:

One option is to use unix2dos (and dos2unix for going back) on the command line.

Another is to use a text editor:
For vi: :set ff=dos to set the line endings to be dos line endings.
For emacs: C-x [ENTER] f dos [ENTER]

For your favourite GUI based editor (eg. jedit) I recommend checking the manual or Google.

Lastly if you don't want to deal with a text editor and just do it using more common utilities and such (or don't have unix2dos installed):

tr -d '\r' < infile > outfile to go from Windows -> Unix
awk 'sub("$", "\r")' unixfile.txt > winfile.txt to go from Unix -> Windows as tr can not go from Unix to Windows.


Solution 3:

Edit it in Vim and use the set fileformat command.

  • MS-DOS/Windows (CR+LF breaks) to *nix (LF only breaks)

    :set fileformat=unix
    :wq
    
  • *nix to MS-DOS/Windows

    :set fileformat=dos
    :wq
    

Solution 4:

This is what I use, similar to Chealion, to convert Windows to Unix line endings:

tr -d \\015 < windows > unix