C:\pg\detect_changes\detect_changes_in_a_directory.sh
DIR_TO_CHECK='/mnt/c/pg/detect_changes'
PATH_TO_EXCLUDE="/mnt/c/pg/detect_changes/_meta*"
OLD_SUM_FILE='/mnt/c/pg/detect_changes/_meta/old_sum.txt'
if [ -e $OLD_SUM_FILE ]
then
OLD_SUM=`cat $OLD_SUM_FILE`
else
OLD_SUM="nothing"
fi
NEW_SUM=`find $DIR_TO_CHECK/* \! -path "$PATH_TO_EXCLUDE" -print0| xargs -0 du -b --time --exclude=$PATH_TO_EXCLUDE | sort -k4,4 | sha1sum | awk '{print $1}'`
if [ "$OLD_SUM" != "$NEW_SUM" ]
then
echo "Directory has changed. Do shomething!"
# do whatever you want to do with the directory.
# update old sum
echo $NEW_SUM > $OLD_SUM_FILE
fi
実行してみます。
$ . detect_changes_in_a_directory.sh
Directory has changed. Do shomething!
$ . detect_changes_in_a_directory.sh
$ touch _smp_.txt
$ . detect_changes_in_a_directory.sh
Directory has changed. Do shomething!
$ . detect_changes_in_a_directory.sh
$ . detect_changes_in_a_directory.sh
$DIR_TO_CHECKに空白スペースが有るとエラーがでます。
findのあとの$DIR_TO_CHECK
は、"$DIR_TO_CHECK"
というように囲んでおいたほうがいいかもしれません。
ref:
– How to detect changes in a directory with Bash — jPablo128