inotify 是 Linux 系统中的一个功能,用于监控文件系统事件
以下是一个使用 inotifywait 命令检测文件删除操作的示例:
inotify-tools 软件包。在基于 Debian 的系统(如 Ubuntu)上,可以使用以下命令安装:sudo apt-get install inotify-tools
在基于 RHEL 的系统(如 CentOS)上,可以使用以下命令安装:
sudo yum install inotify-tools
watch_directory.sh 的 shell 脚本,并添加以下内容:#!/bin/bash
DIRECTORY_TO_WATCH="/path/to/your/directory"
inotifywait -m -r -e delete --format '%w%f' "${DIRECTORY_TO_WATCH}" | while read FILE_DELETED
do
echo "File deleted: ${FILE_DELETED}"
# 在此处执行您的自定义操作,例如备份、记录日志等
done
将 /path/to/your/directory 替换为您要监控的目录路径。
chmod +x watch_directory.sh
./watch_directory.sh
现在,每当有文件从监控的目录中被删除时,脚本都会输出被删除文件的路径。您可以根据需要在 echo 语句之后添加自定义操作,例如备份、记录日志等。