Shell script to check files changed in last 24 hours

Today , i will discuss about the how to get all files which are changed in last  24 hours in shell script.

Below  is the code for the same.

## STEP-1 first of all get the yesterday date using below command.
DATE=`date -d “yesterday” ‘+%Y%m%d%H%M’`
echo $DATE
## STEP-2 Below command will create file date.txt as per yesterday timings.
touch -t $DATE /opt/A1/date.txt

## STEP-3 Below are the paths where we need to check the changed files.Here I have considered four directories.

FILES_PATH_A=”/opt/A”
FILES_PATH_B=”/opt/B”
FILES_PATH_C=”/opt/C”
FILES_PATH_D=”/opt/D”

## STEP-4 Using find command and keyword “newer” , below command will give all files which is newer than date.txt files.

echo “Files changed in $FILES_PATH_A are appended in file changed_ktr.txt”
find “$FILES_PATH_A” -newer /opt/A1/date.txt -type f -name *.ktr > /opt/A1/changed_ktr.txt
echo “Files changed in $FILES_PATH_B are appended in file changed_ktr.txt”
find “$FILES_PATH_B” -newer /opt/A1/date.txt -type f -name *.ktr >> /opt/A1/changed_ktr.txt
echo “Files changed in $FILES_PATH_C are appended in file changed_ktr.txt”
find “$FILES_PATH_C” -newer /opt/A1/date.txt -type f -name *.ktr >> /opt/A1/changed_ktr.txt
echo “Files changed in $FILES_PATH_D are appended in file changed_ktr.txt”
find “$FILES_PATH_D” -newer /opt/A1/date.txt -type f -name *.ktr >> /opt/A1/changed_ktr.txt

## Here, if you observe, i am trying to get all ETL files changed after yesterday timestamp.

echo “Files changed in $FILES_PATH_A are appended in file changed_kjb.txt”
find “$FILES_PATH_A” -newer /opt/A1/date.txt -type f -name *.kjb > /opt/A1/changed_kjb.txt
echo “Files changed in $FILES_PATH_B are appended in file changed_kjb.txt”
find “$FILES_PATH_B” -newer /opt/A1/date.txt -type f -name *.kjb >> /opt/A1/changed_kjb.txt
echo “Files changed in $FILES_PATH_C are appended in file changed_kjb.txt”
find “$FILES_PATH_C” -newer /opt/A1/date.txt -type f -name *.kjb >> /opt/A1/changed_kjb.txt
echo “Files changed in $FILES_PATH_D are appended in file changed_kjb.txt”
find “$FILES_PATH_D” -newer /opt/A1/date.txt -type f -name *.kjb >> /opt/A1/changed_kjb.txt

## So, this script if scheduled on cron tab at particular time,
##it will give all kjb and ktr files on daily basis which are changed in last 1 day.

Related posts