Automated script to audit file presence in a Linux Directory

Saturday, September 13, 2014

Automated script to audit file presence in a Linux Directory

In some cases we may have process that are dependent on a file from external system. There are job schedulers(ex. Autosys) with file watcher functionality which watches for the file and kick off the process automatically, but in some cases we may also need a manual script to do this file existence audit.

Below is a sample script which checks for the presence of a test file(TestFile.txt) in the input directory(INPUTDIR) and will capture the details in a log file (checkFileExists.log) at a user defined log directory (LOGDIR).

Notes
*** Update INPUTDIR, LOGDIR, MAILLIST and file name as per your requirement.
*** If you have multiple files which needs to be audited, create a copy of code in block "Check for file and log the status" and update the file name accordingly.

#  Script Name  : checkFileExists.sh
#  created by      : Entechlog, Sep 2014
# Location of Input File which needs to be checked
export INPUTDIR=/usr/share/Entechlog/input
# Location of log file
export LOGDIR=/usr/share/Entechlog/Log
# Email address to which the audit log need to be mailed
export MAILLIST=your_email_address
# Protect files created by this script.
umask u=rw,g=rw,o=rw
# Delete previous run log file
rm $LOGDIR/checkFileExists.log > $LOGDIR/checkFileExists.log
# Open up a new log for today's run
echo 'checkFileExists.sh File Audit Started' > $LOGDIR/checkFileExists.log
# Append the date to the newly created log file
date >> $LOGDIR/'checkFileExists.log'
# Insert Seperator
echo ' ' >>$LOGDIR/checkFileExists.log
echo '=======================================================' >>$LOGDIR/checkFileExists.log
# Check for file and log the status
if [[ ! -s $INPUTDIR/"TestFile.txt" ]];
then
    echo 'TestFile.txt is NOT available' >>$LOGDIR/checkFileExists.log
else
    echo 'TestFile.txt is available' >>$LOGDIR/checkFileExists.log
fi
# Insert Seperator
echo '=======================================================' >>$LOGDIR/checkFileExists.log
# Email the log file
#mailx -s"File Audit Status" $MAILLIST < $LOGDIR/checkFileExists.log
exit 0;

Sample Audit Output


No comments

Post a Comment

Error 404

The page you were looking for, could not be found. You may have typed the address incorrectly or you may have used an outdated link.

Go to Homepage