-   Programming (
-   -   Inotifywait shell script help - subdirectory depth (

warensemble 02-21-2013 10:12 AM

Inotifywait shell script help - subdirectory depth

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?

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"


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


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

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.