Search code examples
linuxlsansi-escape

Extra escape characters from ls command making life hard


I want to issue a simple ls command and store the results in a file:

ls -ltr equ* > ffile

The file names show up with extra escape characters attached. These show up plainly under some utilities (like vi), and not at all under others (like more). Example:

-rw------- 1 me 9607LPET  72680 Apr 10 12:00 ^[[0m^[[00mequ_00020.dat^[[0m
-rw------- 1 me 9607LPET  72680 Apr 10 12:00 ^[[00mequ_00021.dat^[[0m
-rw------- 1 me 9607LPET 356110 Apr 10 12:00 ^[[00mequ_00065.dat^[[0m
-rw------- 1 me 9607LPET  60253 Apr 10 12:00 ^[[00mequ_00067.dat^[[0m
-rw------- 1 me 9607LPET  83656 Apr 10 12:00 ^[[00mequ_00068.dat^[[0m

Anyway, these characters are detected by almost any tool I might use to process this file list. They are making it very difficult to process the file names. How do I get rid of them? What are they there for in the first place?

EDIT: This question has been flagged as a duplicate of another question that also involved getting rid of colors in the ls output. However, I maintain that getting rid of the colors is a single solution to two distinct problems. The other question is specifically about colors in the first place. This one is not. A user can have the exact problem I just had and still have no idea whatsoever that he should be looking at other questions involving color in ls output. I, of course, was one of these users. I therefore maintain that this is not a proper duplicate of the other question, and this question should be kept as a unique question about a unique problem since that in fact is what it is.


Solution

  • These are the ANSI escape sequences that turn colours on and off. Try adding

    --color=never
    

    to ls to turn colouring off.

    Usually, you should use --color=auto which should turn the colours off when the output is not a terminal.