Find the latest file by modified date

Posted by Rich on Ask Ubuntu See other posts from Ask Ubuntu or by Rich
Published on 2011-09-13T09:22:27Z Indexed on 2012/12/11 17:24 UTC
Read the original article Hit count: 309

Filed under:
|
|
|

If I want to find the latest file (mtime) in a (big) directory containing subdirectories, how would I do it?

Lots of posts I've found suggest some variation of ls -lt | head (amusingly, many suggest ls -ltr | tail which is the same but less efficient) which is fine unless you have subdirectories (I do).

Then again, you could

find . -type f -exec ls -lt \{\} \+ | head

which will definitely do the trick for as many files as can be specified by one command, i.e. if you have a big directory, -exec...\+ will issue separate commands; therefore each group will be sorted by ls within itself but not over the total set; the head will therefore pick up the lastest entry of the first batch.

Any answers?

© Ask Ubuntu or respective owner

Related posts about command-line

Related posts about bash