在 Ubuntu 中,inotify 是一种内核子系统,用于监控文件系统事件
安装 dmenu 和 notify-osd:
对于 Ubuntu 18.04 及更高版本,请安装 dmenu 和 libnotify-bin:
sudo apt-get update
sudo apt-get install dmenu libnotify-bin
对于 Ubuntu 16.04,请安装 dmenu 和 libnotify1.0-bin:
sudo apt-get update
sudo apt-get install dmenu libnotify1.0-bin
创建一个脚本文件,例如 inotify_notifier.sh,并添加以下内容:
#!/bin/bash
# 检查参数数量
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <file/directory> <notification message>"
exit 1
fi
# 获取文件/目录路径和通知消息
FILE_OR_DIR="$1"
NOTIFY_MSG="$2"
# 使用 inotifywait 监控文件/目录
inotifywait -m -e create,delete,modify "$FILE_OR_DIR" |
while read -r event; do
# 当检测到事件时,显示通知
notify-send "Filesystem Change" "$NOTIFY_MSG"
done
使脚本可执行:
chmod +x inotify_notifier.sh
运行脚本:
./inotify_notifier.sh /path/to/file_or_directory "Your custom notification message"
这将监控指定的文件或目录,并在检测到文件系统事件时显示自定义通知消息。
注意:这个脚本只是一个简单的示例,你可以根据需要修改它以满足你的需求。例如,你可以添加更多的 inotify 事件,或者根据事件类型自定义通知消息。