监视文件夹内容更改

我可以使用tail -f命令监视文件的内容更改

有没有类似的方法来监视目录结构的变化,就像tail -f监视文件一样?

我有一个长时间运行的进程,将文件添加到目录下的某个路径,我想跟踪文件incomings,因为它(或他们)写入目录和子目录。

inotify内核系统是您所需要的。

  1. 安装inotify-tools

     sudo apt-get install inotify-tools 
  2. 设置一块手表:

     inotifywait /path/to/directory --recursive --monitor 
  3. 坐下来观看输出。


来自man inotifywait

 -m, --monitor Instead of exiting after receiving a single event, execute indefinitely. The default behaviour is to exit after the first event occurs. -r, --recursive Watch all subdirectories of any directories passed as arguments. Watches will be set up recursively to an unlimited depth. Sym‐ bolic links are not traversed. Newly created subdirectories will also be watched. 

您可以使用--event选项来监视特定事件,例如创建,修改等。

--events不是filter,你必须使用--event 。 例如,这是用于监视创建/修改事件的命令行:

 # inotifywait . --recursive --monitor --event CREATE --event MODIFY 

然后我看到:

 Setting up watches. Beware: since -r was given, this may take a while! 

以下是Feed的格式:

 [path] [event] [file] 

例如

 ./.mozilla/firefox/b4ar08t6.default/ MODIFY cookies.sqlite-wal ./.mozilla/firefox/b4ar08t6.default/ MODIFY cookies.sqlite-wal ./.mozilla/firefox/b4ar08t6.default/ MODIFY cookies.sqlite-wal