find /path/to/dir -type f -mtime -7 -print0 | xargs -0 ls -lt | head
find /path/to/dir -type f -mtime -7 -print0
prints all the files in the directory tree that have been modified within the last 7 days, with null character as the delimiterxargs -0 ls -lt
expects a null delimited list of filenames and will sort the files by modification time, in descending order from most recent to oldesthead
we get the first 10 lines onlyNote that if there are too many files in the output of find
, xargs
will run multiple ls -lt
commands and the output will be incorrect. This is because the maximum command line length is getconf ARG_MAX
and if this is exceeded xargs
has to split the execution to multiple commands. So depending on your use case you may need to tweak the -mtime
parameter to make sure there are not too many lines in the output.
find /path/to/dir -type f | perl -ne 'chomp(@files = <>); my $p = 9; foreach my $f (sort { (stat($a))[$p] <=> (stat($b))[$p] } @files) { print scalar localtime((stat($f))[$p]), "\t", $f, "\n" }' | tail