Bash – Highlight the three last updated files in ls output

bashcommand linelinuxls

Is there any way to overload or wrap the ls command so that it will highlight / underline / otherwise make obvious the last three modified files?

I know that I can simply ls -rtl to order by reverse modification time, but I usually do need an alphabetical list of files despite the fact that I would like to quickly identify the last file that myself or another dev modified.

Best Answer

The following seems to work for me

 grep --color -E -- "$(ls -rtl | tail -n3)|$" <(ls -l)

It uses grep with highlight on input ls -l and uses a regular expression to search for either of the inputs for the three oldest command. It also search for the end-of-line $ in order to print the whole file.

You can also put it in a function, such that you can use lll * with multiple arguments, just as you would use ls

function lll ()
{
    command grep --color -E -- "$(ls -rtl $@ | tail -n3)|$" <(ls -l $@)
}
Related Question