19

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?

Robert Munteanu
  • 1,542
  • 5
  • 22
  • 38

8 Answers8

23

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

Brent
  • 22,219
  • 19
  • 68
  • 102
11

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.

Dennis Williamson
  • 60,515
  • 14
  • 113
  • 148
7

My variation on Brent's answer is:

# du -a | sort -rn | head

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

David Pashley
  • 23,151
  • 2
  • 41
  • 71
3

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.

David Pashley
  • 23,151
  • 2
  • 41
  • 71
2

(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 
rumz
  • 215
  • 1
  • 4
  • 13
2

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

Example

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
1

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
slm
  • 7,355
  • 16
  • 54
  • 72
Gary Chambers
  • 725
  • 4
  • 8
0

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

David Z
  • 5,376
  • 2
  • 24
  • 22