How to convert between Unix and Windows text files PDF Print E-mail
Written by Fernando Amat   
Saturday, 21 January 2012 15:48

Recently I had to switch to a Windows machine. Fortunately, I can install Cygwin and have all the terminal capabilities from Unix machines. I say fortunately because I have many bash scripts to automate routine tasks. However, when I tried to execute the first script the terminal complained that character '\r' in line 2 was not recognized as a command. This message basically indicates the problem that Unix systems and Windows systems use different symbols for 'end of line' or 'carriage return', so Cygwin terminal interprets the end of line from Windows as just another command. This can be easily fixed by using the command d2u (abbreviation for Dos2Unix) that comes with any Unix (or Cygwin system).

 

You just have to type from terminal:

>>d2u filename

 

And the carriage return symbols will be change and all your scripts will work again. This trick also works the other way around: when you have written text files in Unix and open them in Windows Notepad they appear as a long single line. If you run d2u first Notepad will open them appropriately.