Linux bash 用于监视文件更改的脚本 -> 如何获取已更改的文件名
Linux bash script for watching file changes -> how do I get changed file names
我想编辑 Mike Mullin 的这个监视脚本:https://gist.github.com/mikesmullin/6401258 以获取已更改文件的名称。它似乎只检查整个文件列表的数据完整性:
update_sha() {
sha=`ls -lR --time-style=full-iso $path | sha1sum`
}
与旧列表比较:
compare() {
update_sha
if [[ $sha != $previous_sha ]] ; then
echo -n "change detected,"
build
previous_sha=$sha
else
echo -n .
fi
}
如果我理解正确,它只检查是否有任何更改发生,而不是确切更改了哪个文件。
是否还有办法获取已更改文件的单独名称?
为了找出对目录树进行了哪些更改,您需要保存 ls -lr
的整个输出,而不仅仅是校验和。对于多达几百万个文件的目录树,这应该不是什么大问题,但对于非常大的目录树,您可能想尝试不同的方法。
一旦知道两个目录列表不同,就可以使用 diff
实用程序找出不同之处。 Gnu diff 提供了一个非常可定制的机制来格式化 diff 报告。
我可以建议一种不同的方法吗?使用过的工具 "inotifywait"。请参阅此 link 作为参考:https://superuser.com/questions/181517/how-to-execute-a-command-whenever-a-file-changes
刚刚写的这个我认为它可能有用:
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
保存对脚本的更改后的示例输出:
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
将 test.txt
的名称更改为 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
如果检查很多文件,这将是一个缓慢的解决方案。
我想编辑 Mike Mullin 的这个监视脚本:https://gist.github.com/mikesmullin/6401258 以获取已更改文件的名称。它似乎只检查整个文件列表的数据完整性:
update_sha() {
sha=`ls -lR --time-style=full-iso $path | sha1sum`
}
与旧列表比较:
compare() {
update_sha
if [[ $sha != $previous_sha ]] ; then
echo -n "change detected,"
build
previous_sha=$sha
else
echo -n .
fi
}
如果我理解正确,它只检查是否有任何更改发生,而不是确切更改了哪个文件。 是否还有办法获取已更改文件的单独名称?
为了找出对目录树进行了哪些更改,您需要保存 ls -lr
的整个输出,而不仅仅是校验和。对于多达几百万个文件的目录树,这应该不是什么大问题,但对于非常大的目录树,您可能想尝试不同的方法。
一旦知道两个目录列表不同,就可以使用 diff
实用程序找出不同之处。 Gnu diff 提供了一个非常可定制的机制来格式化 diff 报告。
我可以建议一种不同的方法吗?使用过的工具 "inotifywait"。请参阅此 link 作为参考:https://superuser.com/questions/181517/how-to-execute-a-command-whenever-a-file-changes
刚刚写的这个我认为它可能有用:
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
保存对脚本的更改后的示例输出:
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
将 test.txt
的名称更改为 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
如果检查很多文件,这将是一个缓慢的解决方案。