How to use inotifywait to watch files within folder instead of folder

前端 未结 1 1283
北海茫月
北海茫月 2021-01-06 12:01

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

1条回答
  •  时光说笑
    2021-01-06 12:41

    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!

    0 讨论(0)
提交回复
热议问题