Multiple inotify events in one script

Sure, that can be done, with a little bit of extra logic; caveat: this assumes that any of the target directories exist in both /home/test and under /opt and that their names match.

# we use an array to store the watched directories
directories=(/home/test/directory5 /home/test/directory6)
inotifywait -m --format '%w%f' -r -e create "${directories[@]/#/}" | 
    while read spotted; 
            cp "${spotted}" "$archive"
            mv "${spotted}" "$( echo ${spotted}|awk 'BEGIN{FS=OFS="/"}{print "/opt",$(NF-1),$NF }')"

Another observation: should a file of the same name be created in either of your directories the target in archive will be overwritten. You may want to think about that.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top