监视目录列表以进行更改?

sil*_*npi 6 linux directory shell monitoring

在unix系统上,我如何监视(如'tail'的工作方式)目录,以便对文件进行更改 - 创建新文件或更改大小等.

寻找命令行工具而不是要安装的东西.

Gil*_*il' 5

大多数unix变体都有一个API,但它没有标准化.在Linux上,有inotify.在命令行上,您可以使用inotifywait.用法示例:

inotifywait -m /path/to/dir | while read -r dir event name; do
  case $event in
    OPEN) echo "The file $name was created or opened (not necessarily for writing)";;
    WRITE) echo "The file $name was written to";;
    DELETE) echo "The file $name was deleted ";;
  esac
done
Run Code Online (Sandbox Code Playgroud)

Inotify事件类型通常不是您想要注意的(例如OPEN非常宽),所以如果您最终进行自己的文件检查,请不要感到不舒服.


gho*_*g74 1

如果您不想安装工具,您可以自己制作。只是一个想法。使用命令创建目录的基线文件find。使用循环或 cron 作业,find使用相同参数的目录,并根据基线文件检查新文件。使用类似的工具diff 来获取差异。

例如

find /path [other options] >> baseline.txt 
while true #or use a cron job
do
  find /path [same options] >> listing.txt
  diff baseline.txt listing.txt
  # do processing here...
  mv listing.txt baseline.txt  # update the baseline.
  sleep 60
done
Run Code Online (Sandbox Code Playgroud)