Main Content

Moving files between Windows / DOS and Linux / Unix

Archive - Originally posted on "The Horse's Mouth" - 2006-12-30 09:16:48 - Graham Ellis

Text files written on a Windows or DOS operating system use a carriage return character (ASCII decimal equivalent 13) followed by a line feed (ASCII decimal equivalent 10) as their line terminator, but on Linux, Unix and OS X systems, just the line feed character is used. And sometimes you'll find that there's just a carriage return character used - I've seen it this morning with a DreamWeaver generated Style sheet, for example.

If you're transferring text files around, you'll want / need to convert them in some way, but is you're transferring binary files (such as .jpg) , conversion will damage the content beyond redemption.

Here's a tiny utility that I use - a Perl program that converts a text file that's on a Unix / Linux / OS X box from an alien format to the local format.

#!/usr/bin/perl -p
s/\r\n?/\n/g;


Yes - that's it! Makes use of Perl's topicalisation and awk mode command line options. The source code with comments is available here. I called it cv, put it in a directory on my executable path, and made it executable ... and here's a test:

grahamellis$ od -c fiddle.txt
0000000 L i n e 1 \r \n L i n e 2 \r \n
0000020
grahamellis$ cv fiddle.txt > faddle.txt
grahamellis$ od -c faddle.txt
0000000 L i n e 1 \n L i n e 2 \n
0000016
grahamellis$


Other ways of converting ...
• if you transfer files through FTP in ASCII mode, the changes will be made during the transfer (and if you transfer a binary file in ASCII mode, you'll produce a damaged copy!)
• if you read files into certain utilities / editors, they'll convert the input for you silently and / or work in an alien mode. This applies to vim and wordpad, but notepad can't cope.
• Utilities dos2unix and unix2dos are available on some operating systems.