Rom*_*ain 3 bash ubuntu monitoring inotify inotifywait
我想使用 inotifyway 来监视文件夹中新创建或移动的文件,但仅限于文件。
假设我的文件夹名为“watched_folder_test”,我的文件名为“toto.txt”。如果我使用 mv 命令将文件移动到 watched_folder_test 我得到通知我想要
假设在 watched_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
Run Code Online (Sandbox Code Playgroud)
如何获得有关新移动文件夹中的每个文件的通知,而不是文件夹本身的创建?
我不喜欢inotifytools其中包括inotifywait. 我建议用户在使用它时要格外小心,因为在对移动(从和到)目录进行递归监视时,它是完全错误的。
为了表达我的观点,让我们考虑一下您目前遇到的相关情况;目录移动。说,我们正在看 /foo/bar:
上mv /foo/bar /choo/tar,甚至(重命名?)迁出后/foo/bar到/choo/tar,这将继续对报告事件/choo/tar的/foo/bar错误。这是无法接受的!它不应继续监视移出根监视路径的目录。而且,更糟糕的是,它继续以不存在的陈旧路径报告它。
此外,为什么move事件报告为create?太离谱了!Amove完全不同于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
Run Code Online (Sandbox Code Playgroud)
重现您的情况:终端: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
Run Code Online (Sandbox Code Playgroud)
事件日志:终端: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#
Run Code Online (Sandbox Code Playgroud)
不像inotifytools,fluffy 忠实地报告事件!
我希望这个例子足以让你为你的用例加强它。干杯!
| 归档时间: |
|
| 查看次数: |
3405 次 |
| 最近记录: |