May 24, 2012

Strange linefeed characters appearing on Mac

Had a funny situation today where I was emailed a bash program file, but simply downloading the attachment from gmail (in Chrome) added a linefeed (\r) to the file.

This is strange because I never actually opened the file to edit, so I’m not sure what was writing those chars to the file. I’m on a Mac (OSX 10.6.8), so I get the line feed character in general, but just not in this case as the file was never opened.

The following command shows (among other things) the non-printing characters:

od -c filename | sed 4q

You can see the offending \r which was breaking the bash program. The unix tool dos2unix was suggested to clean up the file. Unfortunately this wasn’t available on Mac by default. MacPorts to the rescue (I assume homebrew would be a good option as well):

sudo port install dos2unix
--->  Fetching dos2unix
--->  Attempting to fetch dos2unix-3.1.tar.gz from
--->  Verifying checksum(s) for dos2unix
--->  Extracting dos2unix
--->  Configuring dos2unix
--->  Building dos2unix with target all
--->  Staging dos2unix into destroot
--->  Installing dos2unix 3.1_0
--->  Activating dos2unix 3.1_0
--->  Cleaning dos2unix

Then run the program to clean up the invalid line feed (on 2 files).

dos2unix -n dirwatcher2 dirwatcher2_2
dos2unix: converting file dirwatcher2 to file dirwatcher2_2 in UNIX format ...
dos2unix -n processPDF processPDF_2
dos2unix: converting file processPDF to file processPDF_2 in UNIX format ...

Nice! The changes are:

No \r


© Michael Sharman 2017