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

Viewed 4239

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 ?

1 Answers
Related