Monday, April 29, 2024
66
rated 0 times [  66] [ 0]  / answers: 1 / hits: 145419  / 2 Years ago, fri, april 22, 2022, 1:24:22

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?


More From » command-line

 Answers
4

You do not need to recur to external commands (as ls) because find can do all you need through the -printf action:



find /path -printf '%T+ %p
' | sort -r | head

[#43518] Friday, April 22, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
taigysel

Total Points: 33
Total Questions: 136
Total Answers: 114

Location: Singapore
Member since Wed, Jan 13, 2021
3 Years ago
;