How to color output columns of ls?

This is doable with awk. Unfortunately, since the format of ls -l is unspecified, it is not possible to come up with a solution that will work on every system so some adjustment of which colour to use for which column will be necessary on some systems.


First off, we want to preserve the original spaces used by ls -l. Otherwise, the column alignment will be incorrect. We can do this with the FPAT option, thanks to this SO answer:

ls -la | awk '
    BEGIN {
        FPAT = "([[:space:]]*[^[:space:]]+)";
        OFS = "";
    }

In awk, each positional param ($1, $2, etc.) refers to one field, i.e. one column on the current row. What the FPAT option above did is redefine each field to include all preceding spaces, so when you print it back out it keeps the spaces so the columns stay in the same position.

Now we can simply edit each field to insert the colour code, and then print the edited output:

    {
        $2 = "\033[31m" $2 "\033[0m";
        $3 = "\033[36m" $3 "\033[0m";
        $4 = "\033[36m" $4 "\033[0m";
        $5 = "\033[31m" $5 "\033[0m";
        $6 = "\033[36m" $6 "\033[0m";
        $7 = "\033[36m" $7 "\033[0m";
        $8 = "\033[31m" $8 "\033[0m";
        print
    }
'

Notice that each column is reset back to default (the 0 between the [ and m) afterwards. If you want the colour to run across multiple columns, you can omit that code. Personally, I prefer to specify each column independently.


You can define the a reusable command in your .bashrc. For example:

lsc() {
    ls -la | awk '
        BEGIN {
            FPAT = "([[:space:]]*[^[:space:]]+)";
            OFS = "";
        }
        {
            $2 = "\033[31m" $2 "\033[0m";
            $3 = "\033[36m" $3 "\033[0m";
            $4 = "\033[36m" $4 "\033[0m";
            $5 = "\033[31m" $5 "\033[0m";
            $6 = "\033[36m" $6 "\033[0m";
            $7 = "\033[36m" $7 "\033[0m";
            $8 = "\033[31m" $8 "\033[0m";
            print
        }
    '
}

You may need to restart your bash session (or run source ~/.bashrc) for this function definition to run.

From here, you can just call lsc, which should give you the output you desire:

Screenshot of coloured output


This will print the second column of ls -alF in red, the third through eighth in blue and the ninth in black:

ls -alF | awk -v black=$(tput setaf 0) -v red=$(tput setaf 1) -v blue=$(tput setaf 4) '{$2=red $2; $3= blue $3; $9=black $9} 1'

This is just intended as an example. You may customize this to your heart's content.

For those who prefer their code spread out over multiple lines:

ls -alF | awk -v black=$(tput setaf 0) \
    -v red=$(tput setaf 1) \
    -v blue=$(tput setaf 4) \
    '{
        $2=red $2
        $3= blue $3
        $9=black $9
     }

     1'

Details

tput is a utility to generate a wide variety of control codes for your terminal. For example, tput setaf n sets the foreground color to n where n can range from 0 to 7

0 – Black
1 – Red
2 – Green
3 – Yellow
4 – Blue
5 – Magenta
6 – Cyan
7 – White

You can read more about tput and its color capabilities here.

The awk code defines variables black, red, and blue that contain the terminal codes for black, red, and blue, respectively. These codes are placed at the beginning of whatever column we want to color.

One trick is that color codes don't reset on their own. So, whatever the last color you specify on one line will, unless another color command is specified, be the default color for the next line.

Tags:

Bash

Ls