tags:

views:

38

answers:

4

If I execute ls command with pipe to less, I get strange output

ESC[00mESC[00mfile1.ccESC[00m
ESC[00file2.ccESC[00m
ESC[00file3.ccESC[00m

(means ESC string in between).

Without ls, the output is:

file1.cc    file2.cc    file3.cc

How to correct this?

+1  A: 

I'm guessing that you have the --color=always option to ls set, either through and alias, funtions or the LS_COLORS environment variable and ls is sending color directives to a non-terminal (i.e. your pipe to less).

msw
+1  A: 

What you're seeing are ANSI escape sequences for setting colors. Run ls --color=no.

ninjalj
A: 

Use less -R or set the LESS environment variable to -R.

Jouni K. Seppänen
A: 

You need to make less output raw control characters using less -r.

Michał Trybus
The upper-case option -R avoids some line-breaking problems.
Jouni K. Seppänen
I didn't notice any, but good point.
Michał Trybus