如何在终端中使用 inotify-tools 获取递归目录路径

Posted

技术标签:

【中文标题】如何在终端中使用 inotify-tools 获取递归目录路径【英文标题】:How to get recursive directory path using inotify-tools in terminal 【发布时间】:2013-08-17 13:39:26 【问题描述】:

我正在使用 inotify-tools,我想要一个在递归目录中创建的文件的通知

到这里我就成功了

现在我想获取在递归文件夹中创建/转储文件的目录路径

例如 abc.txt 文件被转储到 data/test 文件夹中

我希望路径是 data/test/abc.txt

下面是我在 .sh 文件中使用的代码

inotifywait -m -r --format '%f' -e modify -e move -e create -e delete /var/www/cloud/data | while read LINE; 
do 
    php /var/www/cloud/scannner/watcher.php; 
done

请帮我在递归目录中获取转储文件的路径

干杯

【问题讨论】:

【参考方案1】:

使用%w 修饰符:

inotifywait -m -r --format '%w%f' .......

要将 inotifywait 的输出作为参数传递给 php 脚本,该脚本将为 argv 变量读取它,您可以这样做:

inotifywait -m -r --format '%w%f' ....... | while read -r line
do 
    php script.php "$line"
done

否则,如果您希望 php 脚本从 standard input 读取 inotifywait 的输出,那么您可以通过管道传输到您的脚本:

inotifywait -m -r --format '%w%f' ....... | php script.php

【讨论】:

Thnx user000001...效果很好请帮助我将 $line 变量传递给 watcher.php 文件 @SaideepKankarla 为此添加了两个解决方案

以上是关于如何在终端中使用 inotify-tools 获取递归目录路径的主要内容,如果未能解决你的问题,请参考以下文章

使用rsync+inotify-tools+ssh实现文件实时同步

使用rsync+inotify-tools+ssh实现文件实时同步

使用inotify-tools与rsync构建实时备份系统

如何在 python 中获取终端输出? [复制]

不在存储库中时如何在终端中获取“git log”或使用 shell 脚本

Linux运维如何在Linux终端获取公有 IP