Modified directory alert team with an email | Bash script

Modified Directory
Modified Directory

In this guide, I have written a bash script which helps to alert the team with email notification on a modified directory or when any changes happened into the directory.

The bash script will store the modified directory information to the CSV file and send that CSV file to the team.

I have written a bash script because the everyday development team asks me for the modified directory information and it’s consumed lots of my time. I decided to write a script that helps me to get the required information and send it to the team.

This is a very simple bash script and you can make changes according to your requirement.

Bash Script | Get Modified Directory Information

#!/bin/bash

##########################################
# Title         : DirStatInfo.sh #
# Usage         : ./DirStatInfo.sh #
##########################################

##VARIABLES TO STORE VALUES

TODAY_DATE=$(date +%Y%m%d)

DIR=conf  [#Change the directory name as per your requirement]

STAT=$(which stat)

COLUMN=$(which column)

MUTT=$(which mutt)

FILENAME=ModifyDirInfo

CSVDIR=/data/dirstatinfo/csvfiles [#Store csv files]

MAILTO='[email protected]'

##Find the Directory with Modify time & Store in csv file.

find /path/ -iname "$DIR" -exec stat -c "%n %y" {} \; > $CSVDIR/$FILENAME.csv

##Cut the column from file and store in separate text file.

cat $CSVDIR/$FILENAME.csv | cut -d' ' -f1 > $CSVDIR/file1.txt

cat $CSVDIR/$FILENAME.csv | cut -d' ' -f2 > $CSVDIR/file2.txt

cat $CSVDIR/$FILENAME.csv | cut -d' ' -f3 > $CSVDIR/file3.txt

##Merge All files in Single CSV file.

cd $CSVDIR/

paste -d, file1.txt file2.txt file3.txt > $CSVDIR/$FILENAME-$TODAY_DATE.csv

##Sed to add 1st column in csv

sed -i '1iDirectoryPath,ModifyDate,ModifyTime' $CSVDIR/$FILENAME-$TODAY_DATE.csv

echo -e "Hello Team,\n\nCSV file generated successfully on server.\n\nPath :: `echo ${CSVDIR} | sed 's: :n:g'`$BODY" | $MUTT -s "'Modify Date & Time for Success direcroty :: ServerName" $MAILTO -a $CSVDIR/$FILENAME-$TODAY_DATE.csv

Change Script File Permission

Once you save the script, provide executable permission to run the script.

$ chmod +x DirStatInfo.sh

Add bash script to the Cron

Now add bash script into Cron so the script will check the modified directory Time after every 30 minutes.

You can change the Cron time according to your requirement.

$ crontab -e

30 * * * * root /bin/bash DirStatInfo.sh

Save and close the file.

This is a well-tested script and provides you data without using more memory on the server.

You can modify the above script according to your requirement. If you still face any issues to setup this script then comments below in the comment box.

Stay connected & subscribe with linuxgrow.com for more such articles.

Prashant

Welcome to Linuxgrow. I'm Prashant, a tech-blogger from Mumbai, India. I started Linuxgrow as a passion and to share my knowledge about technologies. Here at Linuxgrow, I write about Linux technologies, Aws Cloud, Wordpress blogging and scripting knowledge. You can read more about me at About us page. Thank You :)

You may also like...

Leave a Reply

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

Facebook