shell脚本,如何监控目录下的文件内容是否被修改。

第一种方法是通过cmp来进行比对
[root@localhost bo]# ls 1.html 2.html 3.html 4.html 5.html 6.html 7.html 8.html 9.html cat.sh [root@localhost bo]# cat cat.sh #!/bin/bash [ ! -f /root/wyb/bo/cat.log ] && cat *.html > /root/wyb/bo/cat.log cat *.html >tmp.log cmp tmp.log /root/wyb/bo/cat.log [ $? -eq 0 ] && echo yes || echo no [root@localhost bo]# bash cat.sh yes [root@localhost bo]# vi 2.html [root@localhost bo]# bash cat. cat.log cat.sh [root@localhost bo]# bash cat.sh tmp.log /root/wyb/bo/cat.log differ: byte 12, line 3 no [root@localhost bo]#
第二种方法是通过md5来检验
[root@localhost bo]# ls 1.html 2.html 3.html 4.html 5.html 6.html 7.html 8.html 9.html cat.log cat.sh md5.sh tmp.log [root@localhost bo]# cat md5.sh #!/bin/bash LANG=en dosomething() { a=`grep 'FAILED' /tmp/md5.check|awk -F':' '{print $1}'` echo -e "33[32m$a33[0m changed !" } find_file(){ b=`find /root/wyb/bo -type f|xargs md5sum > /tmp/html.md5` echo $b } success() { echo "not change" } [ ! -f /tmp/html.md5 ] && find_file md5sum -c /tmp/html.md5 > /tmp/md5.check 2>&1 [ $? -eq 0 ] && success || dosomething [root@localhost bo]# bash md5.sh not change [root@localhost bo]# echo 3333 >> 3.html [root@localhost bo]# bash md5.sh /root/wyb/bo/3.html changed ! [root@localhost bo]#
原文地址:https://www.cnblogs.com/wangyuebo/p/5866954.html