如何使用 inotifywait 监视文件夹而不是文件夹中的文件

Posted

技术标签:

【中文标题】如何使用 inotifywait 监视文件夹而不是文件夹中的文件【英文标题】:How to use inotifywait to watch files within folder instead of folder 【发布时间】:2017-11-10 14:29:42 【问题描述】:

我想使用 inotifyway 来监控文件夹中新创建或移动的文件但只监控文件。

假设我的文件夹名为“watched_folder_test”,而我的文件名为“toto.txt”。如果我使用 mv 命令将文件移动到watched_folder_test,我会收到我想要的通知

假设在 watch_folder_test 中我有一个名为 foo 的文件夹,我创建了一个文件名“bar.txt”。我收到了我想要的通知。

但这是我的问题。如果我在 watch_folder_test 之外有一个文件夹名称 foo 我在其中有一个文件名 bar.txt ( foo/bar.txt ),我将整个文件夹移到了 watch_folder_test 中。我只收到创建 foo 的通知!与 bar.txt 无关。但是,我并不关心 foo,我只想知道“bar.txt”

这是我目前的代码

#!/bin/bash                                                                                          

inotifywait -mr /home/romain/depot/watched_folder_test -e create -e moved_to |
    while read path action file; do
        echo "The file '$file' appeared in directory '$path' via '$action'"
        for ac in $action
        do
            isdir=`echo $ac | grep 'ISDIR'`
            if [ $? == 0 ]
            then
                echo "It's a folder"
            else
                echo "It's a file"
            fi
        done
    done

如何通知新移动文件夹中的每个文件,而不是创建文件夹本身?

【问题讨论】:

使用stat过滤通知;考虑使用incron 我在 iotifywait 之前尝试过 incron 但未能使用它。此外,当移动或创建目录时,我最终会手动获取目录中的文件。然后我切换到 inotify 的 python 版本。感谢您的反馈 【参考方案1】:

我不是inotifytools 的粉丝,其中包括inotifywait。我建议用户在使用它时要格外小心,因为在移动(从和到)目录上递归监视是完全错误的。

为了表达我的意思,让我们考虑一下您目前遇到的相关情况;目录移动。说,我们在看 /foo/bar:

mv /foo/bar /choo/tar 上,即使在将/foo/bar 移出(重命名?)/choo/tar 后,它仍会继续错误地将/choo/tar 上的事件报告为/foo/bar。这是无法接受的!它不应继续监视已移出根监视路径的目录。而且,更糟糕的是,它继续使用不存在的陈旧路径报告它。

此外,为什么move 事件报告为create?离谱! movecreate 完全不同! movemove,必须报告为 move。很遗憾inotifytools 非常受欢迎,而毫无戒心的用户却没有意识到它的谬误。

现在我已经发泄了沮丧(尽管这很重要),让我们帮助解决您的情况。

运行fluffy:终端:1

root@six-k:/home/lab/fluffy# fluffy | \
while read events path; do \
 if ! echo $events | grep -qie "ISDIR"; then \
  echo "$events $path"; \
 fi
done

重现你的情况:终端:2

root@six-k:/tmp# pwd
/tmp
root@six-k:/tmp# mkdir test
root@six-k:/tmp/test# ls -l
total 0
root@six-k:/tmp/test# mkdir -p d1/dd1
root@six-k:/tmp/test# echo "This file will be moved" | cat >> d1/dd1/f1
root@six-k:/tmp/test# mkdir -p d2/
root@six-k:/tmp/test# ls -l d2
total 0
root@six-k:/tmp/test# fluffyctl -w ./d2

root@six-k:/tmp/test# mv d1 d2/
root@six-k:/tmp/test# ls -lR d1
ls: cannot access d1: No such file or directory
root@six-k:/tmp/test# ls -lR d2
d2:
total 4
drwxr-xr-x 3 root root 4096 Mar 18 20:03 d1

d2/d1:
total 4
drwxr-xr-x 2 root root 4096 Mar 18 20:04 dd1

d2/d1/dd1:
total 4
-rw-r--r-- 1 root root 24 Mar 18 20:04 f1

root@six-k:/tmp/test# echo "Events will be produced on this moved file" | cat >> d2/d1/dd1/f1
root@six-k:/tmp/test# cat d2/d1/dd1/f1
This file will be moved
Events will be produced on this moved file
root@six-k:/tmp/test# echo "New files are also watched in the moved dir" | cat >> d2/d1/dd1/f2
root@six-k:/tmp/test# cat d2/d1/dd1/f2
New files are also watched in the moved dir
root@six-k:/tmp/test# fluffyctl -I d2
root@six-k:/tmp/test# fluffy exit

事件日志:终端:1

root@six-k:/home/lab/fluffy# fluffy | \
> while read events path; do \
>  if ! echo $events | grep -qie "ISDIR"; then \
>   echo "$events $path"; \
>  fi
> done
 
OPEN, /tmp/test/d2/d1/dd1/f1
MODIFY, /tmp/test/d2/d1/dd1/f1
CLOSE_WRITE, /tmp/test/d2/d1/dd1/f1
OPEN, /tmp/test/d2/d1/dd1/f1
ACCESS, /tmp/test/d2/d1/dd1/f1
CLOSE_NOWRITE, /tmp/test/d2/d1/dd1/f1
CREATE, /tmp/test/d2/d1/dd1/f2
OPEN, /tmp/test/d2/d1/dd1/f2
MODIFY, /tmp/test/d2/d1/dd1/f2
CLOSE_WRITE, /tmp/test/d2/d1/dd1/f2
OPEN, /tmp/test/d2/d1/dd1/f2
ACCESS, /tmp/test/d2/d1/dd1/f2
CLOSE_NOWRITE, /tmp/test/d2/d1/dd1/f2
IGNORED,ROOT_IGNORED,WATCH_EMPTY, /tmp/test/d2
IGNORED, /tmp/test/d2/d1
root@six-k:/home/lab/fluffy# 

与 inotifytools 不同,fluffy忠实报告事件!

我希望这个示例足以让您根据自己的用例进行升级。干杯!

【讨论】:

您是fluffy 的创建者吗?这可能应该在答案中注明。 @ReinstateMonica--notmaynard 不,他们不是 @Martin 你确定吗?只需点击发布者个人资料上的链接,即可直接进入以 Fluffy 为主要链接的主页

以上是关于如何使用 inotifywait 监视文件夹而不是文件夹中的文件的主要内容,如果未能解决你的问题,请参考以下文章

在 bash 中使用 inotifywait 监视文件夹以进行队列

使用 inotifywait 监视多个目录并运行脚本

将文件夹添加到监视目录时不会触发 inotifywait close_write

bash 脚本 inotifywait 在继续之前等待文件完全写入

使用 php 执行时 inotifywait 不会传递 args

Linux inotifywait常用参数说明