Shell script to scan size of directories and email when too big

5,679

Solution 1

Are you already running Nagios?

Check out check_dirsize or check_filesize_dir:

http://exchange.nagios.org/directory/Plugins/Uncategorized/Operating-Systems/Linux/CheckDirSize/details

http://exchange.nagios.org/directory/Plugins/Uncategorized/Operating-Systems/Linux/check_filesize_dir/details

Both could be easily adapted to run out of cron if you like.

Solution 2

#!/bin/bash
DIR=/path/to/dir
SIZE=10000
MAILADDR="[email protected]"
if [ $(du -s $DIR | awk '{print $1}') -gt $SIZE ]; then
    echo "$DIR" | mail -s "Directory limit exceeded in $DIR" $MAILADDR
fi

SIZE has to be given in Bytes!

Share:
5,679

Related videos on Youtube

Jayakrishnan T
Author by

Jayakrishnan T

Updated on September 18, 2022

Comments

  • Jayakrishnan T
    Jayakrishnan T over 1 year

    I am at work right now, and my superiors asked me to write/find a shell script for the Red hat Server edition that checks a folder size, and if it's above a certain limit, it will send an email.Can anyone here help me find or help me create a script like that??

    I thank you in advance,

    Jayakrishnan T

    • Sirex
      Sirex about 13 years
      This should get you started: du -sc /path/to/dir/ | grep total | awk '{print $1}' Stick that in a for loop with an if statement and a "mail" command. Be better if you make it send one email with a list of all the dirs that are oversized though, rather than one each.
  • Jayakrishnan T
    Jayakrishnan T about 13 years
    This Script is not working,the file is hanged while executing.
  • Jayakrishnan T
    Jayakrishnan T about 13 years
    This is really a good information,but i question is different.Thanks for your reply
  • noqqe
    noqqe about 13 years
    mh. I tested, but it works on my system. make sure that mail and du is installed and see whats happen use: bash -x ./script to see further details