在Ubuntu系統中,inotify
是一個用于監控文件系統事件的Linux內核子系統。你可以使用inotifywait
命令行工具來檢測文件的修改時間。inotifywait
是inotify-tools
包的一部分,因此首先需要安裝這個包。
inotify-tools
:sudo apt-get update
sudo apt-get install inotify-tools
inotifywait
監控文件修改時間:創建一個名為monitor.sh
的腳本文件,并添加以下內容:
#!/bin/bash
# 監控的文件路徑
file_path="/path/to/your/file"
# 使用inotifywait監控文件修改事件
inotifywait -m -e modify "$file_path" |
while read path action file; do
# 當文件被修改時,獲取文件的修改時間
mod_time=$(stat -c %y "$file_path")
echo "File $file was modified at $mod_time"
done
將/path/to/your/file
替換為你要監控的文件路徑。然后,給予腳本執行權限并運行它:
chmod +x monitor.sh
./monitor.sh
現在,每當文件被修改時,腳本都會輸出文件的修改時間。
注意:inotifywait
默認監控的最大文件描述符數量可能不足以滿足你的需求。如果遇到問題,請嘗試增加fs.inotify.max_user_watches
的值。例如,將其設置為524288:
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p