Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Is there a quick way to convert text file from Unix line breaks to Windows ones in Unix environment? I thought there could be a standard utility.

share|improve this question
add comment

4 Answers

up vote 33 down vote accepted

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.

share|improve this answer
3  
In case anyone comes across this question looking for Windows solutions, the upstream site for dos2unix is waterlan.home.xs4all.nl/dos2unix.html, and it works in Windows as well. –  womble Aug 21 '11 at 3:59
    
You can install dos2unix/unix2dos on Mac OS X using Homebrew. With Homebrew installed, the command is brew install dos2unix, which uses this formula. –  Rory O'Kane Jan 20 '13 at 22:55
add comment

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.

share|improve this answer
    
dos2unix made the trick, however thanks for tr utility, it could have saved so much time in the past! –  alexeit May 8 '09 at 5:06
add comment

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
    
share|improve this answer
    
Be careful when using that if the file does not already have a line break at the last line; vim will add one unless you know how to tell it not to. –  CesarB Jun 14 '09 at 20:54
add comment

This is what I use, similar to Chealion:

tr -d \\015 < windows > unix
share|improve this answer
    
The poster wanted unix to windows line endings. And you're some years late with you answer. –  ott-- Oct 7 '12 at 18:36
5  
Who cares if it's "years late"? It still works, and it's still applicable to the question. I was just adding another option for readers that come upon it, like myself. Also, his question asks for Windows -> Unix, and his description asks for Unix -> Windows. shrug –  Josh Oct 9 '12 at 10:57
    
Right. Maybe you add the other tr-command for converting unix to windows then. –  ott-- Oct 9 '12 at 16:53
    
I tried to undo the downvote, but it said that it's locked until the article is edited. –  ott-- Oct 9 '12 at 21:09
    
@ott-- no problem, I upvoted for you. ...wait, now I can't upvote for me! ...dang... :) –  Alois Mahdal Jul 22 '13 at 0:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.