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 "\033[32m$a\033[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]#
复制代码

 

posted on   王月波  阅读(6357)  评论(0编辑  收藏  举报

编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示