I'm drawing a blank: shell "one-liner" to find "n" largest files in a directory tree
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm drawing a blank: shell "one-liner" to find "n" largest files in a directory tree
Oh, I could figure this out for myself, but maybe it's faster to just ask ...
I need a "shell one-liner" that will tell me, say, the 10 largest files anywhere in a directory or any of its subdirectories. (The 10 largest files with their locations, no matter where they are.)
Last edited by sundialsvcs; 04-12-2022 at 09:50 AM.
Oh ok, you can get rid of the pipes too if one wanted.
Code:
head -n10 <<< $(sort -nr <<< $(du --all --files0-from=<(find . -type f -print0)))
No, that is not true. As it was asked in LQ, this is exactly the same, just with a quite different syntax.
If you really want to avoid pipes you can do something like this (using only one pipe):
Code:
du <arguments> | awk/perl/python 'collect data/sort/print first 20 lines'
To completely avoid pipes you need to implement the directory scan within that python/perl script. It's not that complicated. But those are not one-liners, although you can put them into a script and use as a single command.
On that note, GNU Awk has interesting array sorting...
Code:
cat results-of-du.txt | awk '{a[$2]=$0}END{PROCINFO["sorted_in"]="@val_num_desc";x=0;for (i in a){print(a[i]);if (++x>=10)exit;}}'
(Of course there's not really a one-liner - sure there's no newlines but if a command is longer than 80 characters it's probably disqualified?)
Anyhow, same thing more readably...
Code:
cat results-of-du.txt | awk '''{a[$0]=$1}
END {
PROCINFO["sorted_in"] = "@val_num_desc";
x=0;
for ( i in a )
{
print(i);
if (++x>=10)
exit;
}
}'''
i.e The value of global variable PROCINFO["sorted_in"] defines the order of loop iteration - no need for an explicit sort call - we store sizes in the value, then automatically sort numeric descending on that value, then printing the indexes (which are size+filename; could use "a[$2]=$1" to omit the size).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.