Skip to navigation.
Finding the largest files in a folder

Finding the largest files in a folder

06
Jan
2009

I just needed to find the largest files in a folder (in an attempt to find out why it was so bloody huge!) and have ended up with the following handy combination of commands…

find /path/to/folder -size +1M -print0 | xargs -0 du -h | sort -nr

This simply lists all the files in the path specified where the size is more then 1Mb (you can use k (kilobyte), M (megabyte) and G (gigabyte) too) and prints the list out using null bytes as row terminators. The reason for null bytes is so whitespace characters don't break the next section, xargs. The xargs function allows you to run a command using the piped in value as an argument to the command, in this case we want to know the human readable size of the file. Finally we do a numeric (-n) reverse (-r) sort.

Also finding largest folders

I also needed to find the largest folders in a folder recently and stumbled across a handy gawk script on the linuxquestions.org forum… Check this out!

du -sb * | sort -rn | gawk '{ hum[1024**3]="Gb";hum[1024**2]="Mb";hum[1024]="Kb"; for (x=1024**3; x>=1024; x/=1024) { if ($1 >= x) { printf "%.2f %s \t %s\n", $1 / x, hum[x], $2; break } } }'

Firstly, it does a "Disk Usage summary with bytes size", then pipes that to sort which does a numeric reverse sort. That is then piped into gawk which has a neat little script which does a Kb/Mb/Gb conversion on the bytes "column".

It'd be nice if you asked before taking stuff from my site. Contact me at webmaster [at] thingy - ma - jig . co . uk

This site was based on the Cobalt 2.0 Theme for phpBB written by Jakob Persson

Sponsors
Recent comments
RSS Feed
Search
Follow Us
Weblinks

Add to Technorati Favorites

TGC Webring

CMS Drupal Showcase

Feedburner for ThingyMaJig

View Nicholas Thompson's profile on LinkedIn

Nicholas Thompson's ClaimID

IconBuffet

Twitter

Become A Fan