Convert Unix line endings to Windows

86,328

Solution 1

You can convert them with the unix2dos utility on your Linux platform. There are unix2dos versions available for Windows as well.

If you have Perl installed you can also use this one liner:

perl -p -e 's/\n/\r\n/' < UNIX_LF.txt > WINDOWS_CRLF.txt

Solution 2

Here is an easy and quick way.

Drag and drop the text file into Chrome (I don't know about other browsers) and then cut and paste back into the original file :)

Solution 3

Use the Swiss File Knife.

For example: sfk addcr -dir . -file .txt -norec
changes LF endings into CR/LF for Windows, on all .txt files of the current directory, but NOT within subdirectories (no recursion).

But this program does a lot more than just that.

Solution 4

The one I found best for recursively going through folders, allowing file filters and allowing a simple search for "\r\n" and replacing it with just "\n" was Notepad++.

Notepad++ is one of the best, free, open source notepad programs for Windows. It is very simple and powerful. It handled the line ending search/replace just fine. A contractor check a bunch of .c and .h files in to our repository with Linux \r\n line endings, but since most people have standardized on Windows/Eclipse build tools, the files won't build until the line endings are converted.

Solution 5

On Cygwin, you can convert between Unix and "DOS" AKA Windows files using two built-in utilities:

Convert to DOS CR/LF format:

u2d filename

Convert back to Unix CR format:

d2u filename

The file is left in place with the same name.

Enjoy! Rick

Share:
86,328

Related videos on Youtube

xiaoningyb
Author by

xiaoningyb

Updated on September 17, 2022

Comments

  • xiaoningyb
    xiaoningyb over 1 year

    I recently moved back to Windows from Linux. I have some files with CRLFs, some with LFs and some that are mixed. Is there a utility that will help me find all my Unix-touched files and convert them to proper CRLF terminated files?

    The utility must run on Windows, not Linux. I have already moved. I'd rather not install Cygwin if I can avoid it.

  • xiaoningyb
    xiaoningyb over 14 years
    The problem with this method is that it only does one file at a time.
  • xiaoningyb
    xiaoningyb over 14 years
    'unix-touched files' is my word for both LF-only and mixed CRLF-LF files. Once a file has been edited in unix-oid tools, it tends to get 'infected' with LF-only lines.
  • xiaoningyb
    xiaoningyb over 14 years
    I did something similar to this, but I used python.
  • Almir Sarajčić
    Almir Sarajčić over 14 years
    What I was really saying was that by choosing the right software for viewing text files, they don't have to be converted at all. BTW, I avoided taking you to task for describing CRLF terminated files as 'proper'. It depends entirely on your viewpoint. Each method is proper in its own OS. When a file is moved to a different OS, then we find that Win/Mac/Linux are all different. I suspect that the Unix/Linux method is the original one.
  • quack quixote
    quack quixote over 14 years
    @Matthew Scouten: so... you found a unix utility that runs on windows! congrats!
  • xiaoningyb
    xiaoningyb over 14 years
    yes and no. I WROTE my own blasted utility. I could have done this from the start, but I was hoping to save 30 mins. Unfortunately I was not happy with any utilities available. The closest I found corrupted any binary files in the directory.
  • xiaoningyb
    xiaoningyb almost 14 years
    You suspect wrong. ASCII standard was originally developed for teletype machines, and mandated CRLF. Most internet protocols (including HTTP, SMTP, FTP, IRC and many others) also require CRLF endings (most implementations accept bare LF for compatibility with non-compliant Unix programs). In this case, Windows is following the standard and Unix and Mac are breaking it.
  • xiaoningyb
    xiaoningyb about 13 years
    That's what they said when I had the inverse problem while switching to linux. I kept running into tools that had problems with the wrong line endings. And NOTHING handles mixed line endings reasonably.
  • nelaaro
    nelaaro about 13 years
    @Matthew I know the scite handle unix / windows line endings correctly. I use it for that purpose in the windows vms I run in linux when editing files on the host system.
  • xiaoningyb
    xiaoningyb about 13 years
    I am sure that the editors you recommend will work just fine. but at some point, I will need to feed the textfile to some other tool, and that tool will NOT. At that point, having a text editor that handles things well will make it worse by hiding the problem. and I will bet that it still handles mixed endings badly (or at least with quirks).
  • xiaoningyb
    xiaoningyb about 13 years
    I think you reversed the endings there: linux is \n, windows is \r\n
  • wip
    wip almost 12 years
    +1 for the notepad++ option to convert lie endings. And here is how to do more specifically (see "method 2") : staffwww.fullcoll.edu/brippe/csci123/saveAsUnix.aspx
  • user1071136
    user1071136 almost 12 years
    Notepad++ also has a simple Edit -> EOL Conversion menu if the number of files to convert is small.
  • Csaba Toth
    Csaba Toth over 7 years
    I don't specify -norec because I want recursion to happen but it doesn't seem like it
  • ClintM
    ClintM about 7 years
    hehe… this answer made me lol
  • phuclv
    phuclv about 7 years
    Notepad++ already have the solutions to convert line endings, encodings among other things
  • phuclv
    phuclv almost 6 years
    what's "edit"? there's no such tool in windows 10
  • phuclv
    phuclv almost 6 years
    The OP is asking for a Windows tool
  • phuclv
    phuclv almost 6 years
    all browsers can read Unix line endings just fine, including IE. But even then wordpad can also do that, and so will modern Notepad. What's important here is how to convert multiple files automatically
  • Radek
    Radek almost 6 years
  • phuclv
    phuclv almost 6 years
    obviously I've used edit in DOS but this question is about Windows
  • pbies
    pbies almost 6 years
    There are problems converting with dos2unix and unix2dos - if the file has mixed CRLF and LF there may be some occurences that wouldn't be replaced.
  • Radek
    Radek almost 6 years
    edit used to be part of Windows up to Windows XP. My answer is about 8 years old. In that time edit was part of Windows installation in many cases ...
  • gtatr
    gtatr almost 6 years
    This is sometimes called dos2unix or unix2dos in case you cannot find it as u2d or d2u. There is also unix2mac
  • Eric Duminil
    Eric Duminil about 4 years
    Just don't run it twice on the same file, it would replace \n with \r\r\n.
  • Ramhound
    Ramhound over 3 years
    Please do not ask for upvotes. It is unnecessary and actually considered to be rude. If this answer did not contain an obvious grammatical error. It would receive more positive attention.
  • peterh
    peterh over 3 years
    Really? Please explain, how does this solution change the file contents? Please explain why you believe your solution works.
  • UNLOCKER
    UNLOCKER over 3 years
    I move the text file from linux ubuntu to windows 10 and rename the type to be .txt file. Than I notice the file was 100% converted to .txt with no errors or anything. Just rename the type at the end. Make sure you have enable on windows file type extensions for this to work.
  • phuclv
    phuclv almost 3 years
    rename a file never changes its content. A few only places where it "works" is in git because git does automatic line-ending conversion
  • Tom
    Tom over 2 years
    This does not answer the question - it describes just a workaround.