Wednesday, April 22, 2009

Beware of ls --color on Unix

I ran into a ver interesting problem today.



I was trying to redirect the output of the 'ls' command to a file.



$ ls
1.dat 2.dat 3.dat
$ ls > log




To my surprise the output file contained lots of 'special' characters'.

$ vi log

^[[00m^[[00m1.dat^[[00m^


[[00m2.dat^[[00m^

[[00m3.dat^[[00m^

[[00mlog^[[00m^[[m



This was giving several errors in some other process that was using this file.



After spending an hour on this problem, I figured out that the culprit was an 'alias' that had mapped 'ls' to 'ls --color'. This had caused the output to contain the escape sequences for colors.



The problem got resolved by unaliasing with 'unalias ls'.

No comments: