LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Inotifywait shell script help - subdirectory depth (http://www.linuxquestions.org/questions/programming-9/inotifywait-shell-script-help-subdirectory-depth-4175451120/)

warensemble 02-21-2013 10:12 AM

Inotifywait shell script help - subdirectory depth
 
Hello!

I have inotify tools installed on Ubuntu Server 10.04. I have a script that I want to set up to monitor directories recursively and it starts at boot. This isn't the actual script, just an example. It watches a directory recursively and when an event is triggered copies the file to a destination directory, but only if it's extension is ".txt". How could I modify this to watch recursively, but say up to 4 subdirectories deep?


#!/bin/ksh
WATCHED_DIR="/dir/watcheddir"
DEST_DIR="/dir1/dir2/destdir"
inotifywait -m -r --format '%w%f' -e moved_to -e create $WATCHED_DIR | while read FILE ; do
if [[ "$FILE" == *".txt" ]] ; then
cp -p "$FILE" "$DEST_DIR"
fi
done


Thanks!

pingu 02-21-2013 12:21 PM

I honestly don't think you can do that directly.
A work-around could be to use the --exclude option, but then you can only put one pattern to exclude.
You could modify your script so that the first event is a subroutine that gets the depth, the rest happens only if depth<5.

firstfire 02-22-2013 03:57 AM

Hi.

You can create a list of subdirectories to watch, something like
Code:

DIRS=$(find $WATCHED_DIR -maxdepth 4 -type d)
inotifywait -m --format '%w%f' -e moved_to -e create $DIRS | while read FILE ; do ...


warensemble 02-22-2013 06:36 PM

wonderful, I'll give it a shot. I would not have thought of that. Thanks.

pingu 02-25-2013 04:47 AM

I'm very interested in whether firstfire's solution actually works?
This because AFAIK inotifywait can't watch more than one directory, you'd have to run several instances of inotifywait if you want to watch more than one.
Here $DIRS will contain several directories so I'm rather skeptical.
But it would be great if it works, so please return when you've tested it!


All times are GMT -5. The time now is 02:38 PM.