8

Title kind of says it all: I would like to get those neat colored columns of the output of something like ls -al, say, as in 10basetom’s answer to this question.

Screenshot of syntax-highlighted code snippet from linked answer

  • Well, this is a neat idea! But what OS are you on? – JakeGould Feb 13 '18 at 23:42
  • 2
    That looks to be (incorrectly) Bash syntax-highlighted by the SE software. Looks like it's not coloured by column but rather digits (numbers) get one colour and anything starting with an uppercase get another. Colouring per-column is doable but rather different. – Bob Feb 13 '18 at 23:44
  • 3
    In the general sense, you can split the output on the column separators with sed or awk and insert an ANSI escape sequence. – Bob Feb 13 '18 at 23:46
  • Related: superuser.com/q/665274 – Tyson Feb 13 '18 at 23:53
  • I'm on the Windows 10 Ubuntu subsystem, but transitioning to macOS (given the complete disaster that WinX turned out to be...) – Pirx Feb 14 '18 at 2:21
8

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

  • Awesome guys, this is almost what I want ;-) Seriously, though, I'd like to add one more twist and have the file names colored. ls -al --color=auto would do that, but if I just add the --color option to the solutions above, the color codes seem to get stripped. Can we get awk to keep those? – Pirx Feb 14 '18 at 2:24
  • Second comment, this is more out of curiosity: In the answer I referenced in my original question, it looks like 10basetom did not use any of the above, and still had the colored columns. I wonder if that's because he used a different version of ls, or some fancy smart terminal? – Pirx Feb 14 '18 at 2:32
  • @Pirx The colouring in the answer is applied by the Stack Exchange software. It's not coming from their terminal. It's just because the output was pasted in a code block and the question had a bash tag, which automatically tries to apply Bash syntax highlighting to any code tags in the question and answers ... which is actually incorrect in this case. Same reason the strings in the code are red in my answer, even though I didn't intentionally colour them. – Bob Feb 14 '18 at 2:41
  • 1
    @Pirx Use --color=always. As long as you don't apply different colours to the filename column ($9 in my example), awk should pass them through. – Bob Feb 14 '18 at 2:42
  • @Pirx Side note, --color=always is a GNU extension so if you're on a non-GNU system (e.g. macOS, other BSDs) you'll need to either install the GNU version or use an alternative. – Bob Feb 14 '18 at 2:51
6

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.

  • 1
    Huh, good call on defining variables. Didn't know you could do that from within awk! I'd suggest you include the pattern to preserve column alignment too :) – Bob Feb 14 '18 at 0:27
  • @Bob And, I like your FPAT trick for keeping the spacing correct. – John1024 Feb 14 '18 at 0:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.