I want to solve the problem ‘list the top 10 most recent files in the current directory over 20MB’.
With ls I can do:
ls -Shal |head
to get top 10 largest files, and:
ls -halt |head
to get top 10 most recent files
With find I can do:
find . -size +20M
To list all files over 20MB in the current directory (and subdirectories, which I don’t want).
Is there any way to list the top ten most recent files over a certain size, preferably using ls?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
Method 2
Note: This will work for GNU-find, but not every other find.
find . -maxdepth 1 -size +20M -printf "%<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="471307">[email protected]</a> %fn" |
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.
Method 3
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])
Method 4
I think this might be a better way to solve the problem…
find . -maxdepth 1 -type f -size +20M -exec ls -Shal {} + | head
To get rid of any errors while find tries to access files that you don’t have permission to read you might want to add
find . -maxdepth 1 -type f -size +20M -exec ls -Shal {} + 2> /dev/null | head
Method 5
This will print only the specified 10 files, regardless of any embedded newline chars in file names. It uses file system inodes
cd "$1"
for i in $(find . -maxdepth 1 -type f -size +20M -printf "%<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a8fce8">[email protected]</a>t%in"
| sort -nr | head | cut -f 2)
do
ls -l "$(find . -inum $i)"
done
Method 6
Thanks for all the answers. What I’ve gone for is the following:
ls -halt | grep "M " |head
Although it forgoes the 20 Megabytes in favour of anything over a megabyte (and under a gigabyte), it’s easy to remember and preserves the human readable element of the filesizes.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0