Linux bash script to view file changes & # 8594; how to get changed filenames
I want to edit this clock script by Mike Mullin: https://gist.github.com/mikesmullin/6401258 to get the name of the modified file. Seems to check the data integrity of the entire list of files:
update_sha() { sha=`ls -lR --time-style=full-iso $path | sha1sum` }
Compared to the old list:
compare() {
update_sha
if [[ $sha != $previous_sha ]] ; then
echo -n "change detected,"
build
previous_sha=$sha
else
echo -n .
fi
}
If I understand this correctly, it checks to see if there is any change happening, but not the file that was changed. Is there a way to get the individual name of the file that has changed?
source to share
Can you suggest another approach? Use the "inotifywait" tools. See this link as a reference: https://superuser.com/questions/181517/how-to-execute-a-command-whenever-a-file-changes
source to share
To see what changes have been made to the directory tree, you need to save the entire output ls -lr
, not just the checksum. For a directory tree of up to several million files, this shouldn't be too problematic, but for very large trees, you can try a different approach.
Once you know that the two directories are different, you can use the utility diff
to find the differences. Gnu diff provides a very customizable diff report formatting mechanism.
source to share
Just wrote this, I think it might work:
for name in $(find . -type f)
do
sha1sumstr="$(sha1sum $name)"
sha1sum_sep=($sha1sumstr)
sha1="${sha1sum_sep[0]}"
fp="${sha1sum_sep[1]}"
printf "\nChecking: %s with filepath %s\n" $sha1 $fp
checkstr="$(grep -h $sha1 ../prev_check)"
checkarr=($checkstr)
if [ "$checkstr" == "$sha1sumstr" ]; then
printf "\nnochange\n"
else
printf "Possibly new file:\nFrom: %s\n To: %s\n" "$checkstr" "$sha1sumstr"
if [ "${checkarr[1]}" == "$fp" ]; then
printf "Filname not changed\n"
else
checkname="$(grep -h $fp ../prev_check)"
if [ -n "$checkname" ]; then
printf "$checkname\n"
printf "Name is the same\n"
else
printf "Name Changed: %s\n" "$fp"
fi
fi
fi
echo "$sha1sumstr" >> ../new_check
done
mv ../new_check ../prev_check
Sample output after saving changes to script:
bob@squids:~/Development/fileshare/testdir$ ./check_name_change.sh
Checking: a8fdc205a9f19cc1c7507a60c4f01b13d11d7fd0 with filepath ./test.txt
nochange
Checking: c97a03c8d412ad7a4579fa59b33056253e8113a3 with filepath ./testchilddir/test.txt
nochange
Checking: b8cf155a4ca493f0e8c598e92f212947c13b8842 with filepath ./check_name_change.sh
Possibly new file:
From:
To: b8cf155a4ca493f0e8c598e92f212947c13b8842 ./check_name_change.sh
f46261e680c1f28d7537ac332c02e23fa2e7382b ./check_name_change.sh
Name is the same
Example output after changing the name test.txt
to changed.txt
:
bob@squids:~/Development/fileshare/testdir$ ./check_name_change.sh
Checking: c97a03c8d412ad7a4579fa59b33056253e8113a3 with filepath ./testchilddir/test.txt
nochange
Checking: b8cf155a4ca493f0e8c598e92f212947c13b8842 with filepath ./check_name_change.sh
nochange
Checking: a8fdc205a9f19cc1c7507a60c4f01b13d11d7fd0 with filepath ./changed.txt
Possibly new file:
From: a8fdc205a9f19cc1c7507a60c4f01b13d11d7fd0 ./test.txt
To: a8fdc205a9f19cc1c7507a60c4f01b13d11d7fd0 ./changed.txt
Name Changed: ./changed.txt
This will be a slow solution when checking a large number of files.
source to share