Finding the largest files in a folder

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
[adsense:468x60:4496506397]

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.

Comment Icon

1 Comments

The most recent comment was on Wed, 17th Jun 2009 - 08:11

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

Add new comment

Filtered HTML

  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <img> <p>
  • You can use BBCode tags in the text. URLs will automatically be converted to links.
  • You can enable syntax highlighting of source code with the following tags: <code>, <pre>, <bash>, <css>, <html>, <js>, <jquery>, <mysql>, <php>. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.