监控文件夹内容变化

joh*_*hri 21 filesystem command-line monitoring

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

是否有类似于 tail -f 监视文件的方式监视目录结构更改的方法?

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

mur*_*uru 30

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

  1. 安装inotify-tools

    sudo apt-get install inotify-tools
    
    Run Code Online (Sandbox Code Playgroud)
  2. 设置手表:

    inotifywait /path/to/directory --recursive --monitor
    
    Run Code Online (Sandbox Code Playgroud)
  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.
Run Code Online (Sandbox Code Playgroud)

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


小智 8

--events不是过滤器,你必须使用--event. 例如,这里是监控创建/修改事件的命令行:

# inotifywait . --recursive --monitor --event CREATE --event MODIFY
Run Code Online (Sandbox Code Playgroud)

然后我看到:

Setting up watches.  Beware: since -r was given, this may take a while!
Run Code Online (Sandbox Code Playgroud)

这是提要的格式:

[path] [event] [file]
Run Code Online (Sandbox Code Playgroud)

例如

./.mozilla/firefox/b4ar08t6.default/ MODIFY cookies.sqlite-wal
./.mozilla/firefox/b4ar08t6.default/ MODIFY cookies.sqlite-wal
./.mozilla/firefox/b4ar08t6.default/ MODIFY cookies.sqlite-wal
Run Code Online (Sandbox Code Playgroud)