Welcome!

Linux Authors: Michael Sheehan, Lavenya Dilip, Ian Thain, Bruce Armstrong, Ellen Rubin

Related Topics: Linux

Linux: Article

Ask the AnswerSquad!

Figure out who's hogging up all the disk space

Q:  AnswerSquad, I need your help. Try as I might, I can never seem to keep my users from sucking up all the available disk space. I know I could try using the quota system on my Linux box to keep people in check, but I know my users and know that they'd just get cranky about the system enforcing limits. Instead, I'd like to just have some automated way to send them pestering e-mail if they're using more than their fair share of disk space. But I don't see any existing utility to do this. Help!

One of the classic Unix admin tasks is figuring out who is taking up all the disk space. With MP3 libraries, downloadable movies, and more massive file sources online than we can shake a stick at, it doesn't take much time for that 40GB disk to seem pretty small, does it?

The standard way to calculate disk space usage is to use the du command and have it report on all the subdirectories of your account home directory (often /home):

$ cd /home
$ du -s *
52453 admin
9374 taylor
14325497 mitchell
8498374 citarella

You can get a good idea of disk usage here, but it's not always obvious what the number reports. A quick peek at the du man page reveals that on this version of Linux the numbers are reported in 1K blocks, so admin is using 52MB, taylor's using 9MB, mitchell is eating up lots of space with 14.3GB, and citarella's also grabbing more disk space than might be necessary with 8.49GB. (Actually, some versions of du have a helpful "-h" flag that makes its output more readable. Others have a "-k" flag that force 1K blocks as the unit of calculation.)

Transforming Commands into Shell Scripts

To turn this into a useful shell script - and while bash is the default shell in Linux, we'll use /bin/sh for scripting because it's the standard scripting environment, and so you could copy this script over to any other Unix or Linux system - simply create a plain text file that contains the commands necessary to feed the du output into an awk snippet that can test sizes and calculate MB rather than KB:

cd /home
du -s * | \
awk '$1 > 500000 { MB = $1 / 1024 ; print $2 "=" MB "MB" }'

I'll save this as diskhogs.sh, then use chmod +x diskhogs.sh to ensure it's executable. Now it's easier to figure out who are slackers in disk management:

$ ./diskhogs.sh
mitchell=13989MB
citarella=8299MB

Finally, let's wrap this in a for loop so we can actually generate that complaining e-mail from within the script. First, here's the general structure:

for result in $(du -s * | \
   awk '$1 > 500000 { MB = $1 / 1024 ;print $2 "=" MB "MB" }')
do
   account=$(echo $result | cut -d= -f1)
    usage=$(echo $result | cut -d= -f2)

... now let's generate an e-mail for the disk hog:

done

Not too difficult a sequence. Notice the use of the cut command to pull the first and second field back out of the awk output once in the for loop.

The Final Script

Cleaning things up just a wee bit, here's the final script with a reasonably friendly e-mail message buried within:

#!/bin/sh
cd /home

for result in $(du -s * | \
awk '$1 > 500000 { MB = $1 / 1024 ;print $2 "=" MB "MB" }')
do
account=$(echo $result | cut -d= -f1)
usage=$(echo $result | cut -d= -f2)

# now let's generate an email for the disk hog

cat << EOF | mail -s "Exceeded disk allocation!" $account
To: $account
From: root

You are using more than your fair share of our disk space. All accounts on the system are allocated 500MB of space, but you're currently using $usage space. Please take whatever steps are necessary to reduce your disk usage ASAP, and let me know if you need any help migrating files or data to CD/DVD for archival purposes.

Dave Taylor
administrator
EOF

done

exit 0

Utilizing Cron to Automate the Script

The final step required is to have the script run every night without any further intervention, so you'll attain your goal of bugging disk hogs without any manual effort on your part. Fortunately, cron is a pretty straightforward utility to work with, though a quick glance at the crontab(5) man page is always helpful, where it reminds us that the format for entries is:

field allowed values
----- --------------
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names, see below)
day of week 0-7 (0 or 7 is Sun, or use names)

To have the script run every weekday at 3:00 a.m. then, the fields should be:

0 3 * * 1,2,3,4,5

or, if you want to be a bit more fancy, you can use a range specifier:

0 3 * * 1-5

The last entry on the crontab line is the command to run and you do need to remember that crontab doesn't have a full PATH so you should always specify exactly where the script lives:

0 3 * * 1-5 /home/admin/diskhogs.sh

That's all there is to it. Use the crontab -e command (while logged in as root) to add that line to the root crontab file, and the next weekday morning at 3:00 a.m. mitchell and citarella should both receive e-mail reminders of their disk use problem.

Oh, one more thing. If this script doesn't do the trick, you might find that some of your users are tucking files into directories other than /home, or are assigning ownership of very large files to system accounts to avoid being penalized for owning them. In that case, you'd want to replace the du with a call to find, and you can learn more about that with the more sophisticated version of this script presented as #40, diskhogs, at the Wicked Cool Shell Scripts Web site, www.intuitive.com/wicked.

More Stories By Dave Taylor

Dave Taylor, a contributing editor to Linux.SYS-CON.com, has been involved with the Linux and Unix community since 1980 and has written a number of best-selling Unix books. Currently, he writes, teaches, and works as a management consultant to tech startups, along with his new venture, Ask Dave Taylor!, www.askdavetaylor.com and his personal blog is www.blog.Linux.SYS-CON.com. To contact Dave, please go to /www.intuitive.com/contact.shtml.

Comments (1) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
questionsquad 09/06/07 03:16:03 AM EDT

good job. keep it up.