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:
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!
Related videos on Youtube
Author by
Jayakrishnan T
Updated on September 18, 2022Comments
-
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 about 13 yearsThis 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 about 13 yearsThis Script is not working,the file is hanged while executing.
-
Jayakrishnan T about 13 yearsThis is really a good information,but i question is different.Thanks for your reply
-
noqqe about 13 yearsmh. 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