Wednesday 23 June 2010

Working with DOS formattted files on Ubuntu

I was trying to hack an installation script this morning, and noted that VI was showing me Carriage Return/Line Feed symbols at the end of each line, indicating that the file had last been edited on a Windows platform using a tool such as Notepad.exe.

I can see this because each line ends with the special characters ^M which is the ASCII code for CR/LF.

In the past, I've always used the dos2unix command to strip these out of a file.

However, dos2unix no longer appears to be installed out-of-the-box with Ubuntu 10.04 or, worse still, available via the normal Canonical repositories; I tried installing it using the command: -

sudo apt-get install dos2unix

but got the "Computer says 'NO'" message of: -

E: Couldn't find package dos2unix

Googling around, I found this blog posting: -

http://mediakey.dk/~cc/dos2unix-on-ubuntu-and-debian/

which refers to a different package - tofrodos - which provides similar functionality.

I installed it using a similar command: -

sudo apt-get install tofrodos

accepting the warning that this package isn't certified as being an official part of Ubuntu.

Once done, I get two new commands: -

/usr/bin/todos
/usr/bin/fromdos

which equate to my old friends: -

dos2unix
unix2dos

respectively.

Sweet :-)

2 comments:

Unknown said...

Thank you, I use very often dos2unix and I was really wondering it was! (I use it on the http://piwigo.org project to convert patch files send by contributors running on Windows.

Dave Hay said...

Pierrick - glad to be of use :-)

Visual Studio Code - Wow 🙀

Why did I not know that I can merely hit [cmd] [p]  to bring up a search box allowing me to search my project e.g. a repo cloned from GitHub...