Convert Unix line endings to Windows
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.
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
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.
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.
@quack: The question specifically states that the user is looking for something that will do the job on Windows, using a Windows utility.
14 Answers 14
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
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.
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.
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 🙂
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
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.
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.
On Cygwin, you can convert between Unix and «DOS» AKA Windows files using two built-in utilities:
Convert to DOS CR/LF format:
Convert back to Unix CR format:
The file is left in place with the same name.
This is sometimes called dos2unix or unix2dos in case you cannot find it as u2d or d2u. There is also unix2mac
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
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.
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.
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.
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.
@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.
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).
+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
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’.
Copying text from linux to windows notepad
I would like to know, how to copy the entire lines of text from vi editor in Redhat to a notepad in windows. If the size of the text to be copied is minimal, it can be easily done by selecting the lines through mouse and copying to notepad. However I have come across the situation of copying thousands of lines.
3 Answers 3
I would rather use a file as the media, and use the sz -be coomand send the file to windows.As a suggestion, you can search google about rz/sz command for more details.
If i say straight forward answer then there is no way i guess, but workaround is there,
- Make a public share of samba (not more than 1 min)
- Access from windows
- Create empty file from windows
- At Linux box run
Hope that resolves your problem.
If i say straight forward answer then there is no way i guess. but try below use cases.
- download and install winscp software and connect to your instance (your linux machine) using username and password. Then copy your files from linux to windows.
- if your linux is a virtual machine (running on putty or vmware) using mouse you can directly copy the conent of file in vi editor in linux machine and paste in new or existed file in windows.
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.13.43531
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Linux txt to windows txt
Widows text format and Linux text format
[root @ centos8 data] #cat Linux.txt #linux text file a b c [root @ Centos8 Data] #cat win.txt #windows text file a b c #file command can see the file format (mainly the difference between the end of the line) [[email protected] data]#file win.txt linux.txt win.txt: ASCII text, with CRLF line terminators linux.txt: ASCII text #Linux text file content (hexadecimal) [[email protected] data]#hexdump -C linux.txt 00000000 61 0a 62 0a 63 0a |a.b.c.| 00000006 #windows text file content (hexadecimal) [[email protected] data]#hexdump -C win.txt 00000000 61 0d 0a 62 0d 0a 63 |a..b..c| 00000007 # Use -c View Linux text file content (single-byte character display) [[email protected] ~]# hexdump -c linux.txt 0000000 a \ n b \ n c \ n # This can be observed to observe \ n corresponding to hexadecimal code 0A 0000006 # Use -c View Windows text file content (single-byte character display) [[email protected] ~]# hexdump -c linux.txt 0000000 a, \ r \ n b \ r \ n c \ r \ n # This can be observed to observe \ n correspondence of hexadecimal code 0A 0000007 ## Conclusion: The main difference between Linux and Windows text files is the definition of the carriage. Linux uses \ n as the row end value, and Window uses \ r \ n as the row end.
Linux and Windows text formats conversion
# Install the conversion tool [[email protected] data]#dnf -y install dos2unix # Transferring Windows text format Linux text format [[email protected] data]#dos2unix win.txt dos2unix: converting file win.txt to Unix format. [[email protected] data]#file win.txt win.txt: ASCII text # Convert Linux text format to a text format of Windows [[email protected] data]#unix2dos win.txt unix2dos: converting file win.txt to DOS format. [[email protected] data]#file win.txt win.txt: ASCII text, with CRLF line terminators
Conversion file character set encoding
# Display support character set coding list [[email protected] ~]#iconv -l # Windows7 text default code ANSI (GB2312) [[email protected] data]#file windows.txt windows.txt: ISO-8859 text, with no line terminators # View Linux System Coding [[email protected] data]#echo $LANG en_US.UTF-8 # Default In Linux, the text content is not available properly [[email protected] data]#cat windows.txt ▒▒▒▒▒▒[[email protected] data]# # Convert Windows 7 default encoded ANSI (GB2312) into UTF-8 [[email protected] data]#iconv -f gb2312 windows.txt -o windows1.txt # Modify the encoding and display normal [[email protected] data]#cat windows1.txt Horses Education [root @ Centos8 Data] #LL Windows1.txt -rw-r--r-- 1 root root 12 Mar 23 10:13 windows1.txt [[email protected] data]#file windows1.txt windows1.txt: UTF-8 Unicode text, with no line terminators # Convert UTF-8 into Windows10 text default encoding ANSI (GB2312) [[email protected] data]#iconv -f utf8 -t gb2312 windows1.txt -o windows2.txt [[email protected] data]#file windows2.txt windows2.txt: ISO-8859 text, with no line terminators