Wicked Cool Shell Scripts
Wicked Cool Shell Scripts :: shell script 040-diskhogs.sh

Shell Script 040-diskhogs.sh

#!/bin/sh

# DISKHOGS - Disk quota analysis tool for Unix, assumes all user 
#   accounts are >= UID 100. Emails message to each violating user
#   and reports a summary to the screen

MAXDISKUSAGE=20
violators="/tmp/diskhogs0.$$"

trap "/bin/rm -f $violators" 0

for name in $(cut -d: -f1,3 /etc/passwd | awk -F: '$2 > 99 { print $1 }')
do
  echo -n "$name "
  
  find / /usr /var /Users -user $name -xdev -type f -ls | \
      awk '{ sum += $7 } END { print sum / (1024*1024) }'

done | awk "\$2 > $MAXDISKUSAGE { print \$0 }" > $violators

if [ ! -s $violators ] ; then
  echo "No users exceed the disk quota of ${MAXDISKUSAGE}MB"
  cat $violators
  exit 0
fi

while read account usage ; do

   cat << EOF | fmt | mail -s "Warning: $account Exceeds Quota" $account
Your disk usage is ${usage}MB but you have only been allocated 
${MAXDISKUSAGE}MB.  This means that either you need to delete some of 
your files, compress your files (see 'gzip' or 'bzip2' for powerful and
easy-to-use compression programs), or talk with us about increasing
your disk allocation.

Thanks for your cooperation on this matter.

Dave Taylor @ x554
EOF

  echo "Account $account has $usage MB of disk space. User notified."

done < $violators

exit 0

Explore The Book!
[book cover]
Table of Contents
Read Some Scripts!
Shell Script Library
Book Errata
All The Links
Read the Reviews
Talk About It
Author Bio
Buy The Book!



Other books by author Dave Taylor
Learning Unix for Mac OS X (O'Reilly & Associates)
Solaris 9 for Dummies (Wiley)
Teach Yourself Unix in 24 Hours (Sams/Macmillan)
Teach Yourself Unix System Administration in 24 Hours (Sams/Macmillan)
Creating Cool HTML 4 Web Pages (Wiley)
Ten Quick Steps to Learning Mac OS X Unix (ebook!)