当前位置:网站首页>自动化工具-监测文件的变化
自动化工具-监测文件的变化
2022-06-22 02:24:00 【农民工老王】
博客主页:https://tomcat.blog.csdn.net
博主昵称:农民工老王
主要领域:Java、Linux、K8S
期待大家的关注点赞收藏留言
我最近在使用Linux的过程中遇到,遇到这样一个需求:监测某个文件的创建,变动、删除,并记录文件的每一个版本。我在网上没有找到合适的脚本或工具,然后我就自己写了一个shell脚本实现这个需求。
代码
完整的shell脚本如下,可以直接使用。本示例中,脚本文件名为fileTracer.sh。
#!/bin/bash
# ------------------------------------------
# Filename : fileTracer.sh
# Version : 1.1
# Date : 2022-5-22 00:52:23
# Author : 农民工老王@CSDN
# Email : [email protected]
# Website : https://blog.csdn.net/monarch91
# Description : 用于追踪文件变化的脚本
# ------------------------------------------
time=$1
sleepNum=$2
filePath=$3
fileName=`basename ${
filePath}`
whileNum=$(echo "scale=0;${time}*60/${sleepNum}"|bc)
flag=0
historyDir=./fileHistory
timeStr=""
detection_log() {
timeStr=$(date "+%H:%M:%S.%N")
timeStr=${timeStr:0:12}
echo -e "\033[35m${timeStr}\033[0m \033[36m[DEBUG]\033[0m :$1"
}
existNotice=0
deleteNotice=0
md5StrLast=""
mkdir -p $historyDir
while [ $flag -lt $whileNum ]; do
if [ -f "${filePath}" ]; then
if [ $existNotice -eq 1 ] || [ $flag -eq 0 ] ; then
if [ $flag -eq 0 ]; then
detection_log "文件已存在。"
else
detection_log "文件被创建。"
fi
md5StrLast=`md5sum ${
filePath} | awk '{ print $1 }'`
cp -fr ${filePath} ${historyDir}/${timeStr}_${fileName}
else
md5StrNow=`md5sum ${
filePath} | awk '{ print $1 }'` >/dev/null 2>&1
if [ "lw${md5StrNow}" != "lw" ] && [ "lw${md5StrNow}" != "lw${md5StrLast}" ]; then
detection_log "文件被修改。"
cp -fr ${filePath} ${historyDir}/${timeStr}_${fileName}
md5StrLast=${md5StrNow}
fi
fi
existNotice=0
deleteNotice=1
else
if [ $flag -eq 0 ]; then
detection_log "文件未创建。"
elif [ $deleteNotice -eq 1 ]; then
detection_log "文件被删除。"
fi
deleteNotice=0
existNotice=1
fi
flag=$((flag+1))
sleep ${sleepNum}
done
使用方法
在脚本所在文件夹运行:./fileTracer.sh ${监测时长} ${监测间隔} ${被监测文件的绝对路径}
其中 监测时长 的单位为 分钟,检测间隔的单位为 秒,以上两个参数均可以为小数。如:./fileTracer.sh 5 0.5 /root/test/poem.txt ,此指令表示在未来的5分钟内,每隔0.5秒监测一次 /root/test/poem.txt的文件变化。
如需转载,请注明本文的出处:农民工老王的CSDN博客https://blog.csdn.net/monarch91 。
边栏推荐
- Wechat applet film and television comment exchange platform system graduation design completion (6) opening defense ppt
- Show you how to distinguish several kinds of parallelism
- 微信小程序影视评论交流平台系统毕业设计毕设(6)开题答辩PPT
- Chapter 25 digital watermarking technology based on Wavelet Transform
- A short video costs hundreds of thousands of yuan, and the virtual digital man makes a circle with "strength"
- [essay] the Expo that studied the RN ecology for one day yesterday is really awesome. It works well from development and construction to deployment.
- Rely on the robustness of trusted AI to effectively identify deep forgery and help banks fight identity fraud
- How does the QT program implement the default selection of a behavior in the selected listwidget
- Wechat applet film and television comment exchange platform system graduation design (2) applet function
- Minecraft 1.18.2 生化8 模组 1.3版本 物品3D化+更加复杂村庄
猜你喜欢

EMC輻射發射整改-原理案例分析

MySQL recursively finds the tree structure. This method is very practical!

A short video costs hundreds of thousands of yuan, and the virtual digital man makes a circle with "strength"

Appium interview questions
![LeetCode 513 找树左下角的值[BFS 二叉树] HERODING的LeetCode之路](/img/15/b406e7bf1b83cbdd685c8cde427786.png)
LeetCode 513 找树左下角的值[BFS 二叉树] HERODING的LeetCode之路

Wechat applet film and television review and exchange platform system graduation design completion (8) graduation design thesis template

Annual special analysis of China Mobile Banking in 2022

EMC辐射发射整改-原理案例分析

Leetcode 41 - 45 dynamic planning topic

In 2022, the number of mobile banking users in Q1 will reach 650million, and ESG personal financial product innovation will be strengthened
随机推荐
What does informer have
What is a neural network
MATLAB 学习笔记(5)MATLAB 数据的导入和导出
cmake常用命令分类备忘
2022钎焊考试模拟100题及答案
Zhongang Mining Co., Ltd.: fluorite is a scarce resource with enhanced attributes, and there may be a gap between supply and demand in the future
Fabric. JS iText set italics manually
Using OKR for HR digital transformation
Cmake common command category notes
理财产品赎回确认日是什么意思?
Games-101 personal summary rasterization
Mobile app test method
小孩子学什么编程?
MATLAB 学习笔记(4)MATLAB 数组
数组常用方法
Minecraft 1.18.2 biochemical 8 module version 1.3 3D objects + more complex villages
WinForm project console debugging mode
[Chapter 26 medical impact segmentation system based on minimum error method and region growth -- matlab deep learning practical GUI project]
Annual special analysis of China Mobile Banking in 2022
Chapter 25 digital watermarking technology based on Wavelet Transform

