Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

7 Answers 7

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
share|improve this answer

(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 
share|improve this answer

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

Graphical:

Text-based:

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

share|improve this answer

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

share|improve this answer

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.

share|improve this answer
    
there is a typo, this command should be : find . -maxdepth 2 -type d -exec du -sh {} \; –  Guillaume Gendre Feb 7 '13 at 10:16

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

share|improve this answer
    
Should there be a - before cks? That's the case on Fedora. –  oneodd1 Jun 12 '09 at 21:48
    
yes, there should be. It might be the markdown stealing it. –  David Pashley Jun 12 '09 at 21:55
    
Yes, you are right - corrected it. –  Brent Jun 12 '09 at 22:02
    
Thanks for the answer. I got this far myself, but got stuck on recursion :-) –  Robert Munteanu Jun 12 '09 at 22:32

My variation on Brent's answer is:

# du -a | sort -rn | head

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

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.