如何使用inotifywait监视文件夹而不是文件夹中的文件 [英] How to use inotifywait to watch files within folder instead of folder

查看:392
本文介绍了如何使用inotifywait监视文件夹而不是文件夹中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用inotifyway监视文件夹中新创建或移动的文件,但仅监视文件.

I want to use inotifyway to monitor newly created or moved file within a folder But only the files.

假设我的文件夹名为"watched_folder_test",文件名为"toto.txt".如果我使用mv命令将文件移至watched_folder_test,则会收到我想要的通知

Let's say my folder is name "watched_folder_test" and I have a file name "toto.txt". If I use the mv command to move the file to the watched_folder_test I got notify that I want

比方说,在watched_folder_test中,我有一个名为foo的文件夹,并创建了一个文件名'bar.txt".我收到了想要的通知.

Let's say inside the watched_folder_test I have a folder named foo and I create a file name 'bar.txt". I got the notification that I want.

但这是我的问题.如果我在watched_folder_test之外有一个文件夹名称foo 我里面有一个文件名bar.txt(foo/bar.txt),然后将整个文件夹移到watched_folder_test中.我只收到创建foo的通知!与bar.txt无关.但是,我并不真正关心foo,我只想了解"bar.txt"

But here is my issue. If I have a folder name foo outside of watched_folder_test and I have a file name bar.txt inside it ( foo/bar.txt ) and I move that entire folder inside watched_folder_test. I only get the notification that foo is created ! Nothing about bar.txt. However, I don't really care about foo, I only want to know about "bar.txt"

到目前为止,这是我的代码

here is my code so far

#!/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

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

How can I get notify about every file that within a newly moved folder instead of the creation of the folder itself ?

推荐答案

我不是inotifytools(包括inotifywait)的粉丝.我建议用户在使用它时要格外小心,因为在移入(移入和移入)目录上进行递归监视时,这是完全错误的.

I'm not a fan of inotifytools which includes inotifywait. I would advise users to exercise utmost caution while using it because it is totally erroneous when it comes to recursive watch on moved(from and to) directories.

只是为了阐明我的观点,让我们考虑一下您当前遇到的一种相关情况;目录移动.说,我们正在看/foo/bar:

Just to get my point across, let's consider a relevant situation that you currently have; dir moves. Say, we are watching /foo/bar:

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

On mv /foo/bar /choo/tar, even after moving out(rename?) /foo/bar to /choo/tar, it will continue to report events on /choo/tar as /foo/bar erroneously. This is unacceptable! It should not continue to watch a dir that was moved out of the root watch path. And, to make it worse, it continues to report it with a stale path that doesn't exist.

此外,为什么将move事件报告为create?太离谱了! movecreate完全不同! movemove,必须将其报告为move.可惜的是inotifytools如此流行,而毫无戒心的用户却没有意识到它的谬误.

Besides, why is a move event reported as create? Outrageous! A move is totally different from a create! A move is a move, it must be reported as a move. It's a shame that inotifytools is hugely popular and unsuspecting users are unaware of its fallacies.

现在我已经消除了挫败感(尽管这是相关的),让我们来帮助解决您的问题.

Now that I have vented out the frustration(which is relevant though), let's help fix your situation.

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

Unlike inotifytools, fluffy faithfully reports the events!

我希望这个例子足以使您为您的用例做好准备.干杯!

I hope this example will be sufficient for you to step it up for your use case. Cheers!

这篇关于如何使用inotifywait监视文件夹而不是文件夹中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆