Wednesday, January 28, 2009

Find largest files in Linux


Must be very useful for the Linux newbies.

If you want to find and print the top 10 largest files names (not directories) in a particular directory and its sub directories

$ find . -printf '%s %p\n'|sort -nr|head

To restrict the search to the present directory use "-maxdepth 1" with find.

$ find . -maxdepth 1 -printf '%s %p\n'|sort -nr|head

And to print the top 10 largest "files and directories":

$ du -a . | sort -nr | head

** Use "head -n X" instead of the only "head" above to print the top X largest files (in all the above examples)

Related post:
Sort files and directories by size in Linux

5 comments:

Damon said...

Hey - thanks for this ... I was actually just thinking of a way to look for the sizes of files in a directory tree ...

And, to take it one step further, I wanted to see if I could get a report on the breakdown of file sizes ... for example, the percentage of files that fall into certain file size groups ... like under 4KB, under 1MB, under 1GB, and 1GB+ ... so I could see that: 15% of files are between 1MB and 2MB or something ... reason being, I was going to install a RAID 5 array and wondered which stripe size would be most effective.

Anyway - rambling. Thanks.

Jadu Saikia said...

That would be a great idea, let me give a thought on this.

One clean way would be:

$ find . -printf '%s %p\n'|sort -nr | awk '
$1 < 4096 {++a}
$1 < 1048576 {++b}
$1 < 1073741824 {++c}
$1 > 1073741824 {++d}
END {print a,b,c,d}'

But I am sure we can do it (counting and then %) in a very efficient way using awk. I will give a try and update you.

Halycon said...

Very very handy, this actually helped me find a logfile that was filling up a client's main HD. KUDOS!

Jadu Saikia said...

@Damon,Halycon : thanks for commenting.

Damon said...

Hey - thanks for that ! Awesome. I will try it out tonight.

© Jadu Saikia www.UNIXCL.com