List files larger than {size} sorted by date

The 'current directory' option for find is -maxdepth 1. The whole commandline for your needs is:

find . -maxdepth 1 -type f -size +20M -print0 | xargs -0 ls -Shal | head

or

find . -maxdepth 1 -type f -size +20M -print0 | xargs -0 ls -halt | head

Note: This will work for GNU-find, but not every other find.

find . -maxdepth 1 -size +20M -printf "%T@ %f\n" | 
    sort -nr | head -n 20 | sed 's/[^ ]\+ //'

Starting like @Rajish, but using the printf command from find to specify the output format. There are 3 time-related formats, atime, ctime and mtime - %T is for mtime, %A and %C are for the other formats.

@ is to specify the timeformat in seconds since epoch. %f is for the filename, \n for a newline between 2 files.

Then sorting by number in reversed order gives the youngest files first, and we take 20 lines¹ with head.

In the end, sed is used, to throw away the time information.

¹) since head works line by line, a single file with more than 20 newlines in the name, which is a bit unusual, but not prohibited, can corrupt the output if it is belong the first 20 matches. If you happen to have such files, please try to get rid of them - well, to change their name. They will often be a problem for simple scripts.


Zsh's glob qualifiers make this easy. The . qualifier selects only regular files, Lm+20 selects files that are at least 20MB plus one byte long; to include files that are exactly 20MB long, use L+20971519. Then om sorts by decreasing modification time, and [1,10] restricts the expansion to the first 10 matches. You still need the -t option to ls if you want to list the files by date; or you can pass the files to some other command (youngest first). To pass the files to another command with the oldest file first, use Om to sort by increasing modification time and [-10,-1] to extract the last 10 matches.

ls -ltr -- *(.Lm+20om[1,10])
echo *(.Lm+20Om[-10,-1])

Tags:

Find

Ls