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"
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.
You can create a list of subdirectories to watch, something like
wonderful, I'll give it a shot. I would not have thought of that. Thanks.
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 10:20 AM.|