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.


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 $@)
  • 1
    +1: That's much nicer than the crazy one-liner I eventually came up with: ls -l | grep --color=ALWAYS -C 1000 -E `ls -rt | tail -n 3 | perl -0777 -e '$x=join("|",split(" ",<>));print $x'` – ire_and_curses Sep 27 '12 at 8:02
  • 2
    That is ridiculously epic. :D – whoami Sep 27 '12 at 13:38
  • Awesome work. I'll note that if you try this in a directory that contains less than three files, then I get a "grep: Invalid range end" error. – Jason R Sep 27 '12 at 13:59
  • 1
    @dotancohen You're welcome, thanks for the challenge :) – Bernhard Sep 27 '12 at 17:26
  • 1
    function lll () { command grep --color -E -- "$(ls -rtl $1 | tail -n3)|$" <(ls -l $1) } With this function, you can give a directory as argument, i.e. lll /home @dotancohen – Bernhard Feb 18 '13 at 13:11

It won't highlight the last three modified files (which I think is quite a difficult thing to achieve reliably), but ls++ might be enough for what you want. It highlights files by relative age, grouping by colour files that are seconds, hours, days or months old.

ls++ screenshot

You can configure the colours to make the timescale you tend to work on more apparent (for active coding, hours vs days is the most important cross-over point for me).

  • Nice, thanks. However, I'm usually SSHing into servers that I cannot install packages on. – dotancohen Sep 27 '12 at 12:00
  • 1
    @dotancohen It's a single perl script, you should be able to install it in to your home directory (~/bin or something, add to your PATH in profile) - though this won't work if there are dependencies that are not installed. – Random832 Sep 27 '12 at 14:58
  • I see, thanks Random! In fact I usually do have things in $HOME/bin/. – dotancohen Sep 27 '12 at 16:07

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.