0

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?

RonK
  • 241
  • 1
  • 5
  • 13

1 Answers1

2

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

Ignacio Vazquez-Abrams
  • 45,019
  • 5
  • 78
  • 84
  • 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 05 '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 05 '11 at 11:34
  • @RonK: -t says sort my modification time, -r says reverse the sort order so -1atr does sort and reverse. – user9517 May 05 '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 05 '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 05 '11 at 11:42