LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-21-2013, 09:12 AM   #1
warensemble
LQ Newbie
 
Registered: Feb 2013
Posts: 6

Rep: Reputation: Disabled
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!

Last edited by warensemble; 02-21-2013 at 09:14 AM. Reason: wanted to notate this is for shell scripting
 
Old 02-21-2013, 11:21 AM   #2
pingu
Senior Member
 
Registered: Jul 2004
Location: Skuttunge SWEDEN
Distribution: Debian preferably
Posts: 1,271

Rep: Reputation: 119Reputation: 119
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.
 
Old 02-22-2013, 02:57 AM   #3
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 623

Rep: Reputation: 364Reputation: 364Reputation: 364Reputation: 364
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 ...
 
Old 02-22-2013, 05:36 PM   #4
warensemble
LQ Newbie
 
Registered: Feb 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
wonderful, I'll give it a shot. I would not have thought of that. Thanks.
 
Old 02-25-2013, 03:47 AM   #5
pingu
Senior Member
 
Registered: Jul 2004
Location: Skuttunge SWEDEN
Distribution: Debian preferably
Posts: 1,271

Rep: Reputation: 119Reputation: 119
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to have 2 'exclude' in inotifywait pingu Linux - Software 0 06-06-2012 11:53 AM
Trying to write script to change monitor rez and color depth perlabsrat Linux - Newbie 7 03-31-2009 11:25 AM
script for deleting .dat files which > 5 mb from a directory and its subdirectory anindyabhattacharjee Linux - Enterprise 2 01-15-2007 11:38 PM
where is my subdirectory blotch Linux - Newbie 22 06-18-2005 11:43 AM
need help with a script to automatically create a subdirectory verbatim Linux - Newbie 6 04-26-2005 12:51 AM


All times are GMT -5. The time now is 07:12 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration