Windows to Linux newline conversion (and back)

April 24th, 2007 – hallamigo

As a Linux enthusiast I spend 95% of my computer time using Linux as my desktop, but also have to spend some time on Windows for work to test things, etc - the other 5%. Zipping up my files (lots of text files) and using them between the two platforms can be a nightmare because of the way they both handle newlines. Windows seems to be OK with the way Linux does it, but not the other way around (guaranteed to break your PERL code). If you’ve ever opened a DOS text file on Linux you’ve witnessed how every line ends with ^M which makes the content almost unreadable.

The fix I’ve found is a command line tool called “fromdos”. The name says it all. It also has a sister command called “todos”. I use Slackware as my Linux distro and it came on the standard install available to any non-root user. The main reason I’m posting this little how-to is because when I first attempted to use “fromdos” it didn’t work right; just kind of hung until I canceled it. Here is how you do it - the $> signifies the prompt.

$>fromdos <dosfile.txt> unixfile.txt
or
$>todos <unixfile.txt> dosfile.txt

The most important part to remember and the part I didn’t realize is that the < and > surrounding the first file name need to be there and are part of the command. This is because “fromdos” and “todos” are meant to be used as filters so the “hanging” I experienced was them waiting for input from STDIN.

If you’re not sure what kind of file type you are dealing with (DOS or Unix) you can always run the following command:

$>file filename.txt

Which should provide one of the following two results:

For DOS:
$>filename.txt: ASCII text, with CRLF line terminators

For Unix:
$>filename.txt: ASCII text

At some point I may whip up some sort of a bash file to automate and do batch conversions. Maybe even test the file type first so to not mess with any files that don’t need to be touched. For now though, that little command should save me some time so I don’t end up troubleshooting my PERL script that all the time had the wrong newlines.

ADD TO STUMBLEUPON ADD TO DEL.ICIO.US ADD TO NEWSVINE ADD TO FURL ADD TO NETSCAPE ADD TO TECHNORATI FAVORITES ADD TO SQUIDOO ADD TO WINDOWS LIVE ADD TO YAHOO MYWEB ADD TO ASK ADD TO GOOGLE

Posted in Linux Desktop |

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.