在Ubuntu系统中配置和使用inotify主要涉及安装inotify-tools以及编写和运行监控脚本。以下是详细的步骤:
sudo apt update
sudo apt install inotify-tools
monitor.sh
的脚本文件。nano monitor.sh
#!/bin/bash
# 监控的文件路径
FILE_PATH="/path/to/your/file"
# 使用inotifywait监控文件变化
inotifywait -m -e modify,attrib,close_write,move,create,delete "$FILE_PATH" | while read -r directory events filename; do
# 当文件发生变化时,触发通知
notify-send "File Changed" "The file $filename has been changed."
done
chmod +x monitor.sh
./monitor.sh
-m
:持续监控模式,不断检查文件变化。-e
:指定要监控的事件类型,例如modify(修改)、attrib(属性变化)、close_write(写入关闭)、move(移动)、create(创建)、delete(删除)。notify-send
:发送桌面通知。-r
选项递归监控指定目录及其所有子目录。--format
参数自定义输出格式。0x00000001|0x00000002|0x00000004
表示监控创建、修改和删除事件。uname -r
命令检查内核版本。/proc/sys/fs/inotify/max_user_watches
文件来增加限制。通过以上步骤,你可以在Ubuntu系统上成功配置和使用inotify来监控文件系统的变化。