How can I find the oldest file in a directory tree
This works (updated to incorporate Daniel Andersson's suggestion):
find -type f -printf '%T+ %p\n' | sort | head -n 1
This one's a little more portable and because it doesn't rely on the GNU find
extension -printf
, so it works on BSD / OS X as well:
find . -type f -print0 | xargs -0 ls -ltr | head -n 1
The only downside here is that it's somewhat limited to the size of ARG_MAX
(which should be irrelevant for most newer kernels). So, if there are more than getconf ARG_MAX
characters returned (262,144 on my system), it doesn't give you the correct result. It's also not POSIX-compliant because -print0
and xargs -0
isn't.
Some more solutions to this problem are outlined here: How can I find the latest (newest, earliest, oldest) file in a directory? – Greg's Wiki