網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
Shell腳本實(shí)現(xiàn)監(jiān)測(cè)文件變化的示例詳解_linux shell
作者:農(nóng)民工老王 ? 更新時(shí)間: 2022-08-14 編程語(yǔ)言我最近在使用Linux的過程中遇到,遇到這樣一個(gè)需求:監(jiān)測(cè)某個(gè)文件的創(chuàng)建,變動(dòng)、刪除,并記錄文件的每一個(gè)版本。我在網(wǎng)上沒有找到合適的腳本或工具,然后我就自己寫了一個(gè)shell腳本實(shí)現(xiàn)這個(gè)需求。
代碼
完整的shell腳本如下,可以直接使用。本示例中,腳本文件名為fileTracer.sh。
#!/bin/bash
# ------------------------------------------
# Filename : fileTracer.sh
# Version : 1.1
# Date : 2022-5-22 00:52:23
# Author : 農(nóng)民工老王@CSDN
# Email : scwja@qq.com
# 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 "文件被創(chuàng)建。"
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 "文件未創(chuàng)建。"
elif [ $deleteNotice -eq 1 ]; then
detection_log "文件被刪除。"
fi
deleteNotice=0
existNotice=1
fi
flag=$((flag+1))
sleep ${sleepNum}
done
使用方法
在腳本所在文件夾運(yùn)行:./fileTracer.sh ${監(jiān)測(cè)時(shí)長(zhǎng)} ${監(jiān)測(cè)間隔} ${被監(jiān)測(cè)文件的絕對(duì)路徑}
其中 監(jiān)測(cè)時(shí)長(zhǎng) 的單位為 分鐘,檢測(cè)間隔的單位為 秒,以上兩個(gè)參數(shù)均可以為小數(shù)。如:./fileTracer.sh 5 0.5 /root/test/poem.txt ,此指令表示在未來(lái)的5分鐘內(nèi),每隔0.5秒監(jiān)測(cè)一次 /root/test/poem.txt的文件變化。
原文鏈接:https://blog.csdn.net/monarch91/article/details/124908145
相關(guān)推薦
- 2023-01-28 Shell中的單中括號(hào)和雙中括號(hào)的用法詳解_Linux
- 2022-03-28 Python垃圾回收及Linux?Fork_python
- 2022-11-17 一文講解如何獲取k8s容器里運(yùn)行的jar包_云其它
- 2023-04-29 C++數(shù)組模擬之單鏈表與雙鏈表和棧和隊(duì)列的實(shí)現(xiàn)過程_C 語(yǔ)言
- 2022-09-03 C#設(shè)計(jì)模式之建造者模式_C#教程
- 2022-03-26 Postman傳遞對(duì)象參數(shù)(包含有集合對(duì)象)_相關(guān)技巧
- 2022-04-08 從頭學(xué)習(xí)C語(yǔ)言之字符串處理函數(shù)_C 語(yǔ)言
- 2022-06-06 前端怎么把px單位換成rem單位解決項(xiàng)目頁(yè)面適配問題
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支