Bash script to find the modify date of directory

OVERVIEW

In this post, I am going to provide you a bash script. This bash script will help you to get the details of Modify date, time of any directory on the server.

This modifies data information stored in one CSV file and send this file to your teammates.

The purpose behind writing this script was one of our client uploading some data on our server. And our development team requires the information about when a client is uploading or making any changes to that directory.

This script will get modification information, store it to CSV file and send this CSV file to our development team.

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

#!/bin/bash

###############################################################

# Title         : DirStatInfo.sh                              #

# Version       : 1.0v                              #

# 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='abcd@gmail.com'

##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

PERMISSIONS :

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

$ chmod +x DirStatInfo.sh

HOW TO USE BASH SCRIPT WITH THE CRON:

Now add this script into cron so the script will check the directory ModifyTime 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 well-tested script and provides you data without using more memory on the server.

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

Stay connected & subscribe with techthings.org for more such articles.

Leave a Reply

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