note icon indicating copy to clipboard operation
note copied to clipboard

使用 inotifywait 监控文件变化,并在文件变化后作出操作

Open Yhzhtk opened this issue 11 years ago • 0 comments

使用 inotifywait 监控文件变化,并在文件变化后作出操作

#!/bin/sh
while inotifywait -re create,delete,modify --timefmt '%d/%m/%y/%H:%M' --format '%T %e %w %f' /data/
do
  echo "hello world"
done

inotifywait命令参数

-m是要持续监视变化。 -r使用递归形式监视目录。 -q减少冗余信息,只打印出需要的信息。 -e指定要监视的事件列表。 --timefmt是指定时间的输出格式。 --format指定文件变化的详细信息。

format 指定输出格式。
%w 表示发生事件的目录
%f 表示发生事件的文件
%e 表示发生的事件

可监听的事件

access 访问,读取文件。 modify 修改,文件内容被修改。 attrib 属性,文件元数据被修改。 move 移动,对文件进行移动操作。 create 创建,生成新文件 open 打开,对文件进行打开操作。 close 关闭,对文件进行关闭操作。 delete 删除,文件被删除。

Yhzhtk avatar Jan 23 '14 07:01 Yhzhtk