How to make ls | less colorful?

1k Views Asked by At

Using ls or many other commands, we can get colorful results like this:

enter image description here

But if those results are too long and after we pipe those results to less, the color is disappeared.

This is the result of ls -l | less:

enter image description here

  1. Why the color is disappeared.
  2. How to make the result of ls | less colorful?
1

There are 1 best solutions below

4
On BEST ANSWER

From the man page for ls:

Using the --color option without the optional WHEN argument is equivalent to using --color=always.

With --color=auto, color codes are output only if standard output is connected to a terminal (tty).

Note that always is the default.

From the man page for less:

-r or --raw-control-chars

Causes "raw" control characters to be displayed.

So overall, you need this:

ls -l --color | less -r