Linux: preserving color output on match when piping grep to more/less

As a grep user, you’re probably familiar with the following syntax.

# grep 'something' /some/file --color | more

That ‘–color’ parameter highlights the found text so it’s easier to go through a whole bunch of logs. However, that output disappears as soon as you pipe it to more or less to filter the output into pages.

If you change your command, you can prevent that;

# grep 'something' /some/file --color=always | more

If you just use “–color” grep will determine if the output (your terminal, or the input to a second binary (more/less)) can accept these encodings or not. By default, if you pipe it to another tool it would strip all color.

So, just add “–color=always” and you can pass it to a second binary which will preserve color output!