Sometimes we share our folder with another users for data dumping. At the same time we also want to know when the folder gets modify. Following script is written to alert folder update. This script basically monitors particular folder & sends alters on mail whenever file time stamp modified. This is very basic script using “ls” command.

Copy & save following as /bin/folderchange.sh

#!/bin/bash 
#this script is written to report change in dest folder
#dpthakar@gmail.com
 
PATH=/usr/lib/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
 
LOGFOLDER=/tmp/
SCRIPTFOLDER=/reportchange
#SCRIPTFOLDER=~/reportchange #change it to create reportchange in user home folder.
MAILTO=techienote.com@gmail.com
 
#following is to check log folder
if [ -d $LOGFOLDER ]
        then
                echo "log folder exists, "
        #following is to check script folder to keep old list of files
        if [ -d $SCRIPTFOLDER ]
                then
                        echo "script folder does exists"
                        echo "generating new lists to compare"
                        ls $LOGFOLDER -l | sort > $SCRIPTFOLDER/new_ls
                        echo "comparing backuped & new list"
                        comm -3 $SCRIPTFOLDER/new_ls $SCRIPTFOLDER/old_ls | sed '/total /D' | gawk '{ print $NF}' | uniq > $SCRIPTFOLDER/result_ls
                        echo "backing old list"
                        cat $SCRIPTFOLDER/new_ls > $SCRIPTFOLDER/old_ls
 
                #following is to check if there any change to mail      
                RESULT=`cat $SCRIPTFOLDER/result_ls | wc -l`
                if [ $RESULT -gt "0" ]
                        then
                                echo "mailing change to $MAILTO"
                                cat $SCRIPTFOLDER/result_ls | mail -s "log folder changed -$HOSTNAME"  $MAILTO
                        else
                                echo "no change for mail"
                fi
 
 
 
                else
                        echo "script folder does not exists, creating one"
                        mkdir $SCRIPTFOLDER
                        ls $LOGFOLDER -l | sort > $SCRIPTFOLDER/old_ls
                        echo "required folder / file is created, calling script again"
                        /bin/folderchange.sh
        fi
        else
                echo "pl check if you have mentioned proper log folder to check"
 
fi

You need to add a cronjob entry to check folder updates every five minutes.

crontab -e
0-59/5 * * * * /bin/folderchange.sh

Credits

Monitoring Folder Updates

Leave a Reply

Your email address will not be published. Required fields are marked *