46

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.

6
  • 1
    related: 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 Nov 17, 2009 at 4:59
  • Duplicate question
    – ukanth
    Nov 17, 2009 at 9:40
  • The 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. Nov 17, 2009 at 14:50
  • 1
    they 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. Nov 17, 2009 at 15:00
  • 1
    @quack: The question specifically states that the user is looking for something that will do the job on Windows, using a Windows utility.
    – Brian Lacy
    Nov 19, 2010 at 21:00

14 Answers 14

41

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
5
  • I did something similar to this, but I used python. Nov 19, 2009 at 2:27
  • 2
    @Matthew Scouten: so... you found a unix utility that runs on windows! congrats! Nov 19, 2009 at 13:31
  • 8
    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. Nov 20, 2009 at 15:08
  • 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.
    – pbies
    Jun 16, 2018 at 13:03
  • Just don't run it twice on the same file, it would replace \n with \r\r\n. Apr 8, 2020 at 21:20
14

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 :)

3
  • 2
    hehe… this answer made me lol
    – ClintM
    Feb 20, 2017 at 17:37
  • 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
    – phuclv
    May 29, 2018 at 14:13
  • This does not answer the question - it describes just a workaround.
    – Tom
    Feb 6, 2022 at 11:18
10

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.

2
  • 8
    I think you reversed the endings there: linux is \n, windows is \r\n Apr 8, 2011 at 7:12
  • 10
    Notepad++ also has a simple Edit -> EOL Conversion menu if the number of files to convert is small. Jul 24, 2012 at 22:21
9

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.

1
  • I don't specify -norec because I want recursion to happen but it doesn't seem like it
    – Csaba Toth
    Dec 23, 2016 at 5:23
8

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

1
  • This is sometimes called dos2unix or unix2dos in case you cannot find it as u2d or d2u. There is also unix2mac
    – gtatr
    Jun 19, 2018 at 13:28
5

I'm going to throw this solution out there. Git will do this. See this post about it

So theoretically you could do this to convert an entire tree

cd root/of/tree
git init .
git add .
git commit -m "initial commit"
echo "* text eol=crlf" > .gitattributes
git rm --cached -r .
git reset --hard

Change crlf to lf if you want to go the other way. NOTE: you're not done yet, keep reading

Type git status to see which files will be affected. You might have to add lines like

*.jpg binary
*.png binary
*.gif binary

etc to .gitattributes to avoid converting certain files. You can also explicit mark certain files as text

*.md text
*.css text

Then just repeat these 2 lines after you've edited .gitattributes

git rm --cached -r .
git reset --hard

Then use git status again to see which files will be changed. When you're sure all the files you want affected are listed by git status then commit

git add .
git commit -m "normalize line endings"

now check all the files out again

git rm --cached -r .
git reset --hard

They should now have whatever your desired line endings are

** NOTE: If you were already using git skip the first 3 commands git commands. If you were not using git you can now delete the .gitattributes file and the .git folder.

** Back up your files: the git rm --cached -r deletes them all (although they are theoretically in your git repo (the .git folder) which is how they get restored by the last command git reset --hard. It's just since files are getting deleted it's probably best to back them up.

2

On Wikipedia there's a solution in cmd:

TYPE unix_file | FIND /V "" > dos_file

In PowerShell there are various ways to do that by reversing what was done in this question.

PS> (Get-Content $file -Raw).Replace("`n", "`r`n") | Set-Content $path -Force

PS> (Get-Content $infile) -join "`r`n" > $outfile

PS> $text = [IO.File]::ReadAllText($original_file) -replace "`n", "`r`n"
PS> [IO.File]::WriteAllText($original_file, $text)

It's also possible to do this in VBScript and JScript which are also tools that are already available in Windows without installing a third-party application.

1
  • The PowerShell code snippet helped me. Ta Jan 31, 2023 at 11:05
1

Use a text editor that understands both line endings like SciTE, or Notepad++ if you don't need to convert all line ending in all your files, but just don't want to see the entire file bunched up on the first line.

5
  • 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. Mar 31, 2011 at 17:05
  • @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.
    – nelaaro
    Apr 1, 2011 at 10:54
  • 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). Apr 1, 2011 at 13:44
  • 1
    +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
    – wip
    May 30, 2012 at 1:34
  • Notepad++ already have the solutions to convert line endings, encodings among other things
    – phuclv
    Mar 12, 2017 at 5:41
1

Get AWK for Windows.

Convert Unix line endings to Windows line endings:

awk 'sub("$", "\r")' unixfile.txt > winfile.txt

Convert Windows line endings to Unix line endings:

awk '{ sub("\r$", ""); print }' winfile.txt > unixfile.txt

Older versions of awk do not include the sub function. In such cases, use the same command, but replace awk with gawk or nawk.

Source

0

There are many ways to translate the eoln characters in TEXT files, and everyone has a favourite.

But I always transfer files from Linux to Windows in BINARY mode, then I open TEXT files in Windows with an editor capable of opening both types, and saving them in either form, if necessary.

I used Programmers File Editor for this, but Notepad++ can do it too. WordPad is also useful [at least, for viewing LF terminated files].

I'm thinking of the simple text files which may have originated on Linux, and need to be readable in the (defacto standard) world of Windows. I'm not sure what you meant by 'unix-touched files'.

4
  • The problem with this method is that it only does one file at a time. Nov 19, 2009 at 2:23
  • '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. Nov 19, 2009 at 2:26
  • 2
    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.
    – pavium
    Nov 19, 2009 at 2:38
  • 3
    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. Jul 16, 2010 at 4:06
0

To offer more options (even though I best enjoyed user45832's hack answer):

Online convertors

I guess a GUI FTP program will do it automatically

Batch/DOS one-liner (removes empty lines):

FOR /F "eol= delims= usebackq" %a IN (infile.txt) DO (ECHO %a>> outfile.txt)

Powershell:

gc infile.txt | %{$_.split("`n")} | Out-File outfile.txt

Got the last two from here

-1

My Linux distribution has two little utilities called fromdos and todos that I use for this task.

1
  • The OP is asking for a Windows tool
    – phuclv
    May 29, 2018 at 14:08
-2

I used to open the file in 'edit' and save as that the job was done ...

4
  • what's "edit"? there's no such tool in windows 10
    – phuclv
    May 29, 2018 at 14:02
  • computerhope.com/edithlp.htm
    – Radek
    Jun 16, 2018 at 12:45
  • obviously I've used edit in DOS but this question is about Windows
    – phuclv
    Jun 16, 2018 at 12:51
  • 2
    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 ...
    – Radek
    Jun 18, 2018 at 11:57
-2

For me in windows10 on 2020 I just rename the file from "file" to "file.txt" Best solution I guess.

5
  • 1
    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.
    – Ramhound
    Dec 11, 2020 at 1:17
  • 1
    Really? Please explain, how does this solution change the file contents? Please explain why you believe your solution works.
    – peterh
    Jan 20, 2021 at 10:23
  • 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.
    – UNLOCKER
    Jan 21, 2021 at 18:54
  • rename a file never changes its content. A few only places where it "works" is in git because git does automatic line-ending conversion
    – phuclv
    Aug 7, 2021 at 1:37
  • my answer is for those want to convert their Linux text files to windows txt files and believe it needs an utility converter when they need just to rename the file to .txt
    – UNLOCKER
    Nov 7, 2022 at 3:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .