Search code examples
bashls

ls -l with some modifications


I wanted to do some changes to the ls -l command.

First, I wanted to remove the first "total X" line as I don't use it.

Then, I wanted to show directories topmost. (Or, more precisely, sort according to type.)

Third, I wanted to sort according to name in cases of identical type.

This would be almost trivial to write in a script or functional language, or even in C, but I got inspired by something I read that you should not write new stuff for everything, but rather combine the tools you have. Some hours (!!) later, I came up with this:

ls -l | tac | head -n -1 | sort -k 1.1,1.2 -k 9

Feeling a bit silly, it seems to work. But:

Slower:

real 0m0.009s
user 0m0.008s
sys 0m0.008s

instead of ls -l:

real 0m0.003s
user 0m0.004s
sys 0m0.000s

No colors! (With ls -l, dictionaries are blue, etc.)

So, if you know of a better way, shoot :)


Solution

  • man ls, I mean really, try it.

    For 2nd, 3rd and no colors

    $> ls -l --group-directories-first --sort=extension --color=never
    

    If you need colors you should use --color=auto or --color=always.

    For the 1st question there is a duplicate, for example.

    Actually, you can remove first line with awk:

    awk '{ if ( NR > 1 ) print }'
    

    So resultant command looks like

    $> ls -l --group-directories-first --sort=extension --color=always | awk '{ if ( NR > 1 ) print }'