Using a shell script to mail directory changes alerts

05 Aug 2005 20:36 - (0) comments

Here is a handy little script I made to send yourself email's alerting you about changes to important directories. For example track changes made to your mac Drop Box: ~/Scripts/check_dir_mailer.sh ~/Public/DropBox/ {YOUR_EMAIL}. Schedule this in your crontab (crontab -e) and you'll be automatically alerted when someone uploads a file to your Drop Box.

I renamed my Drop Box folder to DropBox to avoid troubles with spaces in names.

#!/bin/bash
# If anything's changed to the given directory send an email to the email adress
DIR=$1
timestamp=".timestampChecker"

# check for correct usage
if [ $# -lt 1 ] ; then
echo "Usage: $0 dir emailaddress " >&2
exit 1
fi

# check if the local dir exists
if [ ! -d $DIR ] ; then
echo "$0: Error: directory $DIR doesn't exist?" >&2
exit 1
fi
cd $DIR

# count files to ensure something's changed:
if [ ! -f $timestamp ] ; then
>> $timestamp #create a new timestamp file
else
count=$(find . -newer $timestamp -type f -print | wc -l)
fi

touch $timestamp
if [ $count -ne 0 ] ; then
echo "Mailing $2 "
`ls -a "$DIR" | mail -s "WARNING: someone dropped a file in your $DIR" $2`
exit 0
fi

Comments

No comments allowed.

Admin