It seems that ls doesn’t sort the files correctly when doing a recursive call:
ls -altR . | head -n 3
How can I find the most recently modified file in a directory (including subdirectories)?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
For a huge tree, it might be hard for
sortto keep everything in memory.%T@gives you the modification time like a unix timestamp,sort -nsorts numerically,tail -1takes the last line (highest timestamp),cut -f2 -d" "cuts away the first field (the timestamp) from the output.Edit: Just as
-printfis probably GNU-only, ajreals usage ofstat -cis too. Although it is possible to do the same on BSD, the options for formatting is different (-f "%m %N"it would seem)And I missed the part of plural; if you want more then the latest file, just bump up the tail argument.