finding largest directories in a filesystem

I'm trying to come up with a way of finding largest directories in a filesystem (let's say filesystems is running ot of space and I need to find what is consuming all the space). If a directory is a single filesystem, then it's easy, I just run "du -sk *| sort -nr". But the problem is, if some subdirectories are on separate filesystems, then it will show those as well. It's especially hard to get filesystem usage on / since I have many filesystems on some boxes. I tried "du -skx *" but it still shows directories in child filesystems. Then I tried to do it with find command:

find . -xdev -type d -exec du -sk {} \; | sort -nr | head -10 

Despite -xdev switch, I still get the child filesystems in the output. Is there another way of accomplishing this? I'm a bit stumped at the moment. Any advice will be appreciated!

man du (-x)

Replace "*" with the name of the largest filesystem.

du -kx /fs

Hi,

You can check the largest files under directories and subdirectories by using the following command.

ls -lR | sort +4 -5nr | head -10

R -- will check the files under subdirectories

Thanks,
Aketi

Thanks, this works! Gives me a bit too much info though (I only really want to see parent directories, not subdirectories):

:/]# du -kx / |sort -nr|head -10
5312843 /
4527973 /usr
1951796 /usr/lib
1735880 /usr/share
952800  /usr/lib/ooo-1.1
743964  /usr/lib/ooo-1.1/program
613476  /usr/lib/ooo-1.1/program/resource
381064  /usr/share/doc
310004  /opt
284383  /var

I threw together a little script, maybe there's a better way to do it but seems like it works:

:/]# for DIR in `ls -1`; do mount | grep /"$DIR" | grep -v /"$DIR"/ > /dev/null || du -skx $DIR; done |sort -nr|head
4527973 usr
310004  opt
284383  var
85468   lib
62344   etc
19056   RedHat
15580   sbin
1668    tmp
468     media
200     dev

The ls in backticks is not really Useful. I guess you can also cut out the second grep by anchoring the end of the search string.

for DIR in *; do
   mount | grep /"$DIR$" > /dev/null || du -skx "$DIR";
done |sort -nr|head

Thanks... I was originally using "find" and when I realized it wasn't doing me any good I switched to "ls". I forgot I can just use a wild card :wink:

The grep situation is a bit tricky. I'm really looking for a string in the "mount" output that does "not" have / at the end. For example, if I'm at / and have a filesystem mounted at /ks-images/rhel4, I still want to include /ks-images in my output. So came up with a stupid trick of first looking for "/ks-images" and then looking for anything that doesn't match "/ks-images/". "$" doesn't help, in fact the mount point is not even at the end of the line. I'm sure there's a better way, I could use help with this...

du -kx / | awk -F/ 'NF==2'

wow - this works very nicely! I would have never thought of counting the fields. Thanks so much for your help! :b: