Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
add comment

1 Answer 1

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
1  
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
show 1 more comment

Your Answer

 
discard

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.