find newest file

Caporegime
Joined
18 Oct 2002
Posts
29,493
Location
Back in East London
I've got a restore script for restoring the latest .tar.gz of some directories, but the script we're using parses output from
Code:
ls -1t
and takes the first line. Just wondered if there was a straight up command for finding newest file.

Ta. :)
 
Last edited:
Not that I know of:
Code:
ls -lt /path/to/dir/ | head -2 | tail -1 | awk '{print $8}'
will do it quite simply though.
 
Last edited:
We're already using:
Code:
ls -1t /path/ | head -1
which works better :) Was just wondering if there was a "hidden" ls option or something. :)
 
We're already using:
Code:
ls -1t /path/ | head -1
which works better :) Was just wondering if there was a "hidden" ls option or something. :)

That shouldn't work at all - it will just give the total in that directory unless your ls binary is different..

Not that I know of. If your a c programmer look at stat() ( http://www.opengroup.org/onlinepubs/000095399/functions/stat.html ) You could prolly write a custom program to do it easily and quicker than ls if performance is an issue.
 
Last edited:
Back
Top Bottom