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.

14 Answers14

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
phuclv
  • 30,396
  • 15
  • 136
  • 260
16

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

user45832
  • 177
11

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.

Matt
  • 111
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.

harrymc
  • 498,455
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

Gareth
  • 19,080
Rick V
  • 81
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 "rn" > $outfile

PS> $text = [IO.File]::ReadAllText($original_file) -replace "n&quot;, &quot;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.

phuclv
  • 30,396
  • 15
  • 136
  • 260
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

phuclv
  • 30,396
  • 15
  • 136
  • 260
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.

nelaaro
  • 14,139
  • 30
  • 88
  • 115
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'.

pavium
  • 6,490
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

gregg
  • 6,307
-1

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

phuclv
  • 30,396
  • 15
  • 136
  • 260
geek
  • 10,674
-2

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

-2

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

Radek
  • 3,134