Linux tip: Hunt the disk hog

Browse your file system for heavy usage quickly with a handy alias

It always seems to happen late on a Saturday night. You're getting paged because a partition on one of the servers (probably the mail server) is dangerously close to full.

Obviously, running a df will show what's left:

rob@magic:~$ df

Filesystem 1k-blocks Used Available Use% Mounted on

/dev/sda1 7040696 1813680 4863600 27% /

/dev/sda2 17496684 13197760 3410132 79% /home

/dev/sdb1 8388608 8360723 27885 100% /var/spool/mail

But you already knew that the mail spool was full (hence, the page that took you away from an otherwise pleasant, non-mailserver related evening). How can you quickly find out who's hogging all of the space?

Here's a one-liner that's handy to have in your .profile:

alias ducks='du -cks * |sort -rn |head -11'

Once this alias is in place, running ducks in any directory will show you the total in use, followed by the top 10 disk hogs, in descending order. It recurses subdirectories, which is very handy (but can take a long time to run on a heavily loaded server, or in a directory with many subdirectories and files in it). Let's get to the bottom of this:

Linux Server Hacks: 100 Industrial-Strength Tips and Tools

rob@magic:~$ cd /var/spool/mail

rob@magic:/var/spool/mail$ ducks

8388608 total

1537216 rob

55120 phil

48800 raw

43175 hagbard

36804 mal

30439 eris

30212 ferris

26042 nick

22464 rachael

22412 valis

Oops! It looks like my mail spool runneth over. Boy, I have orders of magnitude more mail than any other user. I'd better do something about that, such as appropriate new hardware and upgrade the /var/spool/mail partition. ;)

As this command recurses subdirectories, it's also good for running a periodic report on home directory usage:

root@magic:/home# ducks

[ several seconds later]

13197880 total

2266480 ferris

1877064 valis

1692660 hagbard

1338992 raw

1137024 nick

1001576 rob

925620 phil

870552 shared

607740 mal

564628 eris

For running simple spot checks while looking for disk hogs, ducks can save many keystrokes (although if we called it something like ds, it would save even more, but wouldn't be nearly as funny.)

This tip is excerpted from Linux Server Hacks: 100 Industrial-Strength Tips and Tools, published by O'Reilly & Associates, ISBN: 0-596-00461-3, all rights reserved.

Copyright © 2003 IDG Communications, Inc.

7 inconvenient truths about the hybrid work trend
Shop Tech Products at Amazon