I want to use inotifyway to monitor newly created or moved file within a folder But only the files.
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
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.
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 ?
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.
Just to get my point across, let's consider a relevant situation that you currently have; dir moves. Say, we are watching /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.
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.
run fluffy: terminal: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
Reproduce your situation: terminal: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
Event log: terminal: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#
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!
来源:https://stackoverflow.com/questions/47225008/how-to-use-inotifywait-to-watch-files-within-folder-instead-of-folder