views:

525

answers:

3

Looking for a command that will return the single most recent file in a directory.

Not seeing a limit parameter to ls...

+7  A: 
ls -Art | tail -n 1

Not very elegant, but it works.

dmckee
perfect thanks!
AK
This doesn't include files that start with .
Jared Oberhaus
@Jared Oberhaus: To true. Fixed. Thanks.
dmckee
+2  A: 
ls -c|head -n1
chaos
Equivalent to mine, and possibly more efficient, too.
dmckee
doesn't quite work for me since i echo out some info in my ls first, but i get the usage, thanks!
AK
+3  A: 

ls -lAtr | tail -1

The other solutions do not include files that start with '.'.

This command will also include '.' and '..', which may or may not be what you want:

ls -latr | tail -1

Jared Oberhaus
Will this one return "." if the directory has been modified more recently than any file contained (say by a deletion)? Maybe that is the desired behavior, maybe not. But you're right either way.
dmckee
Yes, it does return '.' in that case, good point.
Jared Oberhaus