I'm looking for a script/program which will display the top x largest directories/files and then descend into those folders and display the x largest directories/files for a configurable depth.

231MB bin
 - 220MB ls
  - 190MB dir
  - 15MB def
  - 3MB lpr
 - 10MB asd
 - 1MB link

How can I do that?


8 Answers 8


You can see the 10 largest directories with:

du -cks *|sort -rn|head

This will recursively add up the sizes of everything in each directory - but you would have to manually execute it at each level to get a breakdown of what's in each


Chances are your system has one of these installed or available through your package manager:



  • ncdu - ncurses
  • gt5 - text browser (lynx, w3m, etc. - auto-selected) - It's actually a shell script!

They may not work exactly as you specified, but they should do most of what you need.


My variation on Brent's answer is:

# du -a | sort -rn | head

Which will give you the largest directories or files in the tree.

  • I would add the -h option to get better printed results for the size.
    – f.ederi.co
    Jun 12, 2009 at 22:03
  • 2
    You can't add -h as then you'd end up with 2M being higher than 1G. Jun 12, 2009 at 22:22
  • Thanks for the answer. I got this far myself, but got stuck on recursion :-) Jun 12, 2009 at 22:33
  • 2
    GNU sort also supports the -h option. Apr 22, 2013 at 1:31

You could do some similar using find:

# find -maxdepth 2 -type d -exec du -sh {} \;

But this won't be sorted by size and will be incredible slow and inefficient. Better off writing a script that parses du -a.

  • there is a typo, this command should be : find . -maxdepth 2 -type d -exec du -sh {} \; Feb 7, 2013 at 10:16

This gets you the sorted human-readable size of the top 10 largest files/folders in the entire directory tree:

du -ah | sort -hr | head


1019M   .
431M    ./prod
195M    ./prod/mainprogram
180M    ./utils
162M    ./prod/subprogram1
157M    ./.git
156M    ./py
155M    ./.git/objects
148M    ./.git/objects/pack
128M    ./prod/mainprogram/hdf

(Wanted to add this as a comment to Brent's answer but dont see a link to do that) I recently had a similar problem, and if you need to just check one partition in question, adding a -x to Brent's answer is pretty useful, especially if some directories have their own partition.

So it would be:

du -ckx / | sort -rn | head 

Something to remember about human-readable output is that a reverse numeric sort won't work as expected without fixing it up. A quicker option is to use the -k switch to output the directory size information in kilobytes.

I'd have a short look into the manpage of sort - on some systems you can use sort -h to sort human-readable output. I've used this on CentOS 6.2 with sort 8.4. Otherwise you can redirect the output of du -h to a file and sort that with something like:

for i in K M G; do egrep "^[0-9,\.]+${i}" somefile | sort -n; done

I don't know of a command-line tool that does this (other than what the other answers have suggested), but if you are able to run GUI programs on this machine, try KDirStat. It shows the disk usage of all files and directories under a particular root, sorted by size (by default).

Not the answer you're looking for? Browse other questions tagged .