在Ubuntu中,你可以使用inotify-tools来检测文件移动
inotify-tools。如果没有,请运行以下命令来安装:sudo apt-get update
sudo apt-get install inotify-tools
inotify_move_detector.sh的脚本文件,并在其中输入以下内容:#!/bin/bash
# 检查参数数量
if [ "$#" -ne 3 ]; then
echo "Usage: $0 <watch_directory> <output_file> <timeout>"
exit 1
fi
# 设置变量
WATCH_DIR=$1
OUTPUT_FILE=$2
TIMEOUT=$3
# 使用inotifywait监视目录
inotifywait -m -r -e moved_to,moved_from --format '%w%f %e' --timefmt '%Y-%m-%d %H:%M:%S' "${WATCH_DIR}" |
while read FILE EVENT; do
echo "$(date +"%Y-%m-%d %H:%M:%S") - ${FILE} - ${EVENT}" >> "${OUTPUT_FILE}"
done &
PID=$!
# 等待超时或脚本被终止
wait $PID || kill $PID
exit 0
chmod +x inotify_move_detector.sh
/home/user/documents目录,并在output.log文件中记录事件,设置超时时间为60秒:./inotify_move_detector.sh /home/user/documents output.log 60
脚本将监视指定的目录,并在检测到文件移动时将相关信息记录到输出文件中。当超时时间到达或脚本被终止时,脚本将自动停止监视。
注意:inotifywait可能会消耗大量系统资源,特别是在监视大型目录时。因此,请谨慎使用,并根据需要调整超时时间。