LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-12-2022, 09:48 AM   #1
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,691
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
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.
 
Old 04-12-2022, 09:54 AM   #2
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,152
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
Something like this, or a variant.
Code:
find . -type f -print0 | xargs -0 du | sort -n | tail -20
Edit:
Or jam as many pipes in there that you wish
Code:
find . -type f -print0 | xargs -0 du | sort -n | tail -20 | cut -f2 | xargs -I{} du -sh {}

Last edited by teckk; 04-12-2022 at 09:59 AM.
 
Old 04-12-2022, 10:03 AM   #3
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,627

Rep: Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556

Don't need xargs, but do need --all to prevent du grouping files into directories?
Code:
du --all --files0-from=<(find ./directory -type f -print0) | sort -nr | head -n10
 
Old 04-12-2022, 10:22 AM   #4
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,152
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
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)))
 
Old 04-12-2022, 10:28 AM   #5
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,152
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
And to answer the OP ,you would search the whole file tree. You'll need to be root for some of it.
Code:
head -n10 <<< $(sort -nr <<< $(du --all --files0-from=<(find / -type f -print0)))
 
Old 04-12-2022, 10:44 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,357
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
I'd add that sort has a -h option which can work with the output from du and it's own -h option.
 
Old 04-12-2022, 01:25 PM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,039

Rep: Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347
Quote:
Originally Posted by teckk View Post
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.
 
Old 04-12-2022, 05:01 PM   #8
!!!
Member
 
Registered: Jan 2017
Location: Fremont, CA, USA
Distribution: Trying any&ALL on old/minimal
Posts: 997

Rep: Reputation: 382Reputation: 382Reputation: 382Reputation: 382
https://askubuntu.com/questions/1292...es-in-a-folder
 
Old 04-12-2022, 05:20 PM   #9
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,627

Rep: Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556

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).

 
Old 04-13-2022, 12:28 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,039

Rep: Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347
Quote:
Originally Posted by boughtonp View Post
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;}}'
UUoC, you only need:
Code:
f='{a[$2]=$0}END{PROCINFO["sorted_in"]="@val_num_desc";x=0;for (i in a){print(a[i]);if (++x>=10)exit;}}'
awk "$f" results-of-du.txt
 
Old 04-13-2022, 08:00 AM   #11
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,627

Rep: Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556
Quote:
Originally Posted by pan64 View Post
UUoC, you only need:
Yes, I know!

I was maintaining the structure of post #7 whilst providing a runnable command (and deliberately not using newlines).

 
Old 04-13-2022, 09:55 AM   #12
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,691

Original Poster
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
Thanks, folks. (I didn't mean to create a "Name That Tune" competition!)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Gab becomes the largest Mastadon node, bringing the largest user contribution to the fediverse LXer Syndicated Linux News 0 07-06-2019 03:20 PM
find the largest and smallest file in a directory and sub directory schandran Programming 5 05-06-2015 09:53 AM
Cannot assign variable in shell one liner mojofender Linux - Newbie 4 12-12-2013 07:32 PM
LXer: Simple Shell One-Liner To Enumerate File Types In Linux and Unix LXer Syndicated Linux News 2 05-30-2008 08:47 AM
Find 100 largest files in folder tree tyreth Linux - General 2 02-07-2006 08:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration