Convert Unix line endings to Windows
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
Related videos on Youtube
xiaoningyb
Updated on September 17, 2022Comments
-
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.
-
quack quixote over 14 yearsrelated: see superuser.com/questions/38744 superuser.com/questions/27060 superuser.com/questions/52044 ... any tool mentioned in those can be reversed to accomplish what you want to do
-
ukanth over 14 yearsDuplicate question
-
xiaoningyb over 14 yearsThe solutions in those so called Duplicate questions all run on linux platforms or preform the opposite conversion. I don't have linux any more. I have windows.
-
quack quixote over 14 yearsthey are all available for Windows via Cygwin, GnuWin32, UnxUtils or the like. i believe there are Powershell scripts that can do this as well, though i don't know enough about it to provide any links.
-
Brian Lacy over 13 years@quack: The question specifically states that the user is looking for something that will do the job on Windows, using a Windows utility.
-
phuclv about 7 years
-
-
xiaoningyb over 14 yearsThe problem with this method is that it only does one file at a time.
-
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 over 14 yearsI did something similar to this, but I used python.
-
Almir Sarajčić over 14 yearsWhat 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 over 14 years@Matthew Scouten: so... you found a unix utility that runs on windows! congrats!
-
xiaoningyb over 14 yearsyes 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 almost 14 yearsYou 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 about 13 yearsThat'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 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 about 13 yearsI 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 about 13 yearsI think you reversed the endings there: linux is \n, windows is \r\n
-
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 almost 12 yearsNotepad++ also has a simple
Edit -> EOL Conversion
menu if the number of files to convert is small. -
Csaba Toth over 7 yearsI don't specify
-norec
because I want recursion to happen but it doesn't seem like it -
ClintM about 7 yearshehe… this answer made me lol
-
phuclv about 7 yearsNotepad++ already have the solutions to convert line endings, encodings among other things
-
phuclv almost 6 yearswhat's "edit"? there's no such tool in windows 10
-
phuclv almost 6 yearsThe OP is asking for a Windows tool
-
phuclv almost 6 yearsall 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 almost 6 years
-
phuclv almost 6 yearsobviously I've used
edit
in DOS but this question is about Windows -
pbies almost 6 yearsThere 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 almost 6 years
edit
used to be part of Windows up toWindows XP
. My answer is about 8 years old. In that timeedit
was part of Windows installation in many cases ... -
gtatr almost 6 yearsThis is sometimes called
dos2unix
orunix2dos
in case you cannot find it as u2d or d2u. There is alsounix2mac
-
Eric Duminil about 4 yearsJust don't run it twice on the same file, it would replace
\n
with\r\r\n
. -
Ramhound over 3 yearsPlease 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 over 3 yearsReally? Please explain, how does this solution change the file contents? Please explain why you believe your solution works.
-
UNLOCKER over 3 yearsI 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 almost 3 yearsrename a file never changes its content. A few only places where it "works" is in git because git does automatic line-ending conversion
-
Tom over 2 yearsThis does not answer the question - it describes just a workaround.