Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make ls | less colorful?

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?
like image 904
Yishu Fang Avatar asked Jan 05 '14 02:01

Yishu Fang


People also ask

How do I get my ls color?

You need to pass --color option to the ls command on Linux. If you are using OS X or BSD based system pass -G option to the ls command. The syntax is as follows to turn on or off colors.

How do you remove dye from ls?

Color output for ls is typically enabled through an alias in most distros nowadays. You can always disable an alias temporarily by prefixing it with a backslash. Doing the above will short circuit the alias just for this one invocation. You can use it any time you want to disable any alias.


1 Answers

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
like image 154
Oliver Charlesworth Avatar answered Oct 03 '22 22:10

Oliver Charlesworth