3

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

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

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

但这是我的问题。如果我在watched_folder_test 之外有一个文件夹名称foo,并且里面有一个文件名bar.txt (foo/bar.txt),我将整个文件夹移动到watched_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

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

4

1 回答 1

6

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

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

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

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

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

运行蓬松:终端: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 忠实地报告事件!

我希望这个例子足以让你为你的用例加强它。干杯!

于 2018-03-18T14:47:53.180 回答