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

第一种方法是通过cmp来进行比对[[email protected] bo]# ls
1.html  2.html  3.html  4.html  5.html  6.html  7.html  8.html  9.html  cat.sh
[[email protected] 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
[[email protected] bo]# bash cat.sh
yes
[[email protected] bo]# vi 2.html
[[email protected] bo]# bash cat.
cat.log  cat.sh
[[email protected] bo]# bash cat.sh
tmp.log /root/wyb/bo/cat.log differ: byte 12, line 3
no
[[email protected] bo]# 
第二种方法是通过md5来检验[[email protected] 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
[[email protected] 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

[[email protected] bo]# bash md5.sh 

not change
[[email protected] bo]# echo 3333 >> 3.html
[[email protected] bo]# bash md5.sh
/root/wyb/bo/3.html changed !
[[email protected] bo]# 
时间: 2024-08-07 23:09:26

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

windows下查找一个目录下所有文件内容

遇到这个问题的背景是我反编译了一个apk,得到了它的source code.我在jd-gui中查找一个String时,发现查找结果严重不全,于是我想,如果不用jd-gui自带的搜索功能,而直接从源码文件夹中查找这个String就好了. 但windows自带的查找功能,只能查找目录下的文件名,而无法查找文件内容.总不能把文件一个一个都打开,然后挨个find一遍吧? 办法总是有的,答案就是万能的notepad++(我爱notpad++): 首先ctrl+f,出现文件查找的框以后,点击第三个tab,就

XPS 工程目录下各文件内容

XPS Project Directories To view the XPS directory structure, refer to XPS Directory Structure Outline. __xps Contains intermediate files generated by XPS and various other tools for internal management purpose. You should not use this directory, but

解决往监控目录拖拽文件夹无法监控到的问题

在项目中一个应用场景是监控一个目录变化,但从监控目录外部拖拽一个文件夹进来,拖拽进来的文件夹里的文件无法监控到,通过查看资料,发现是一个参数的设置 #include <iostream> #include "DirectoryChanges.h" #include "MyDirectoryChangeHandler.h" int main() { wcout.imbue(locale("CHS")); CDirectoryChange

AssetsUtils【读取assets、res/raw、./data/data/包名/目录下的文件】

版权声明:本文为博主原创文章,未经博主允许不得转载. 前言 封装了以下功能: 1.读取assets目录下的资源html.文件.图片,将文件复制到SD卡目录中: 2.读取res/raw目录下的文件内容: 3.读写./data/data/包名/下的文件: 效果图 代码分析 最常用的方法:getStringFromAssert(Context mContext, String assetsFilePath)[读取assets目录中的文件字符串] 使用步骤 一.项目组织结构图 注意事项: 1.  导入类

java利用WatchService实时监控某个目录下的文件变化并按行解析(注:附源代码)

首先说下需求:通过ftp上传约定格式的文件到服务器指定目录下,应用程序能实时监控该目录下文件变化,如果上传的文件格式符合要求,将将按照每一行读取解析再写入到数据库,解析完之后再将文件改名. 一. 一开始的思路 设置一个定时任务,每隔一分钟读取下指定目录下的文件变化,如果有满足格式的文件,就进行解析. 这种方式很繁琐,而且效率低,效率都消耗在了遍历.保存状态.对比状态上了! 而且无法利用OS的很多功能. 二. WatchService介绍 1. 该类的对象就是操作系统原生的文件系统监控器!我们都知

监控web站点目录下所有文件是否被恶意篡改

监控web站点目录下所有文件是否被恶意篡改,(文件内容被改了)如果有的就打印改动的文件名 定时任务:每三分钟执行一次监测一次 文件被篡改的特征: 大小可能会变化(为什么说可能呢,如果把里面的值1改为2大小是不会变化的) 修改时间会变化   (文件测试符ot,nt) 文件内容会变化,通过md5sum指纹判断 增加或者删除文件 问题: 代码发布方案:大公司或规范的公司,不会时刻传代码,每天1-2次 脚本不严谨啊,工作中先解决文件,然后在解决好问题 #!/bin/sh num=`cat site.lo

C# 利用FTP自动下载xml文件后利用 FileSystemWatcher 监控目录下文件变化并自动更新数据库

using FtpLib; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.IO; using System.Linq; using System.ServiceProcess; using System.Text; using System.Threading; using

Linux下使用shell实现上传linux下某个目录下所有文件到ftp

首先我们需要搞清楚单个文件怎么上传,把这个单文件上传到ftp上的实现命名为一个:upload_to_ftp_command.sh 之后,需要弄清楚怎么实现遍历一个目录下的所有文件的,把这个遍历某个目录下的文件实现命名为:foeach_directory_and_uploadfile_to_ftp.sh. upload_to_ftp_command.sh #!/bin/bash FTILE_NAME=$1 ftp -n <<- EOF open 100.170.141.26 user jy ne

将二级目录下的文件合并成一个文件的Python小脚本

这个小程序的目的是将二级目录下的文件全部合并成一个文件(其实几级目录都可以,只要做少许改动) 1 #coding:utf8 2 import sys, os 3 4 def process(path): 5 new_file = open("file_1", "a+") 6 for secDir in os.listdir(path): 7 for f in os.listdir(path + "/" + secDir): 8 fin = ope