Adding a bit of color to your linux shell commands

By Steve Claridge on 2014-03-15.

Unix command shell output can often be hard to read. If you are running commands that produce lots of output text it can be hard to read and hard to find the strings in the output that you are interested in. Plain old Grep is useful for showing just the lines you are interested in, but even that has a drawback: a single line of output can be very long and wrap around multiple lines on the screen, which still results in a lot of output to sift through.

To show specific strings in a different color you can use grep's ---color parameter along with a smart regular expression. For example, here's a snippet of output from the ps aux command:

I want to highlight the processes owned by Apache so I don't have to scan up and down the long list, so I pipe the ps aux output through grep using this:

ps aux | grep -E ---color=always '^|apache'

And I get this:

Piping output from any command through grep is pretty straightforward, one thing to note is the regular expression being used:


The goal of grep here is not to reduce the output but to show everything and to show the matched strings in a different color. Using just 'apache' wouldn't work as it would only display the lines containing apache , so we add the ^ at the start so that our regex matches the start of every line and also the word apache.

You can of course use this technique to chain Grep commands and color in grep output to make searching through big logfiles easier:

cat server.log | grep "ats.internal" | grep -E ---color=always '^|first pass'