Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to create an alias that displays the latest log in a certain directory - something like: ls -lat *log* | tail -1 | less

The above command does not work as the output of the ls -lat command does not produce a single final name but a full list of columns.

Any simple solution?

share|improve this question
up vote 2 down vote accepted

Get rid of the -l and use -1 instead. Really, it's all in the book.

share|improve this answer
ls -1at is more like it, as Ignacio says, but you probably want to use head -1 instead of tail if I understood 'latest' correctly. You can otherwise use ls -1atr | tail -1, but the first option is better for long directories. – Eduardo Ivanec May 5 '11 at 11:28
@Eduardo: Thanks for the comment - Are you sure that ls -1at will be faster than ls -1atr ? Won't it take the same amount of resources from the system? – RonK May 5 '11 at 11:34
@RonK: -t says sort my modification time, -r says reverse the sort order so -1atr does sort and reverse. – Iain May 5 '11 at 11:37
Also head only processes the first line, tail must process the whole stream and discard everything except the last line - they are very optimized so I may be splitting hairs though : ) – Eduardo Ivanec May 5 '11 at 11:40
I checked and on a somewhat long dir piping to head -1 cuts time (real) on half as opposed to piping to tail or doing no pipes at all. It's a bad benchmark but I'm semi-convinced. Also ls -1atr and ls -1at take the same amount of time, perhaps the sort condition is reversed instead of sorting and then reversing - which makes sense of course. – Eduardo Ivanec May 5 '11 at 11:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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