LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 03-25-2009, 10:49 PM   #1
Techno Guy
Member
 
Registered: Dec 2008
Posts: 47

Rep: Reputation: 15
bash script that will check folder x for new folders.


Is it possible to have a script that will check in say folder x1 to see if there are any new folders in x1 since last check, and if so run a .sh script in the new folder/s
(this checking script would of course be run every x mins via cron.)

If this is possible, can someone give me some hints into the right direction?
(possible commands to use etc...)

That would be fantasic
 
Old 03-26-2009, 04:08 AM   #2
roy_lt_69
Member
 
Registered: Aug 2006
Location: Vancouver, BC, Canada
Distribution: Slackware, Bodhi, Mepis, AntiX, Mint
Posts: 172

Rep: Reputation: 21
Sure, it is possible.
But is this another class assignment?

Hmmm, look up touch and find for starters!
 
Old 03-26-2009, 04:49 AM   #3
Techno Guy
Member
 
Registered: Dec 2008
Posts: 47

Original Poster
Rep: Reputation: 15
Ah thanks, Roy, ill give them 2 a try in the weekend.

And no this isn't for any assignment, I'm doing C# at the moment for an assignment thou (I don't like C# much thou)
 
Old 03-29-2009, 03:40 AM   #4
Techno Guy
Member
 
Registered: Dec 2008
Posts: 47

Original Poster
Rep: Reputation: 15
Ok, so far I have this:

Code:
#!/bin/bash

for f in $( find ./ -type d -mmin -1 ); do
	echo $f
	/demotimedmkdir.sh $f
done
Which kinda does what I wanted, but it goes into all folders in ./ but I want it to only loop though the folders in ./ not all the subfolders.
Any thoughts?

Thanks again for all your help!
 
Old 03-29-2009, 07:14 AM   #5
mangezoutianya
LQ Newbie
 
Registered: Feb 2009
Distribution: Debian
Posts: 4

Rep: Reputation: 1
If you wanted to search something in fixed levels of subdirectories, use -maxdepth and -mindepth with the "find" command. For example, find something just in the current folder, you can use:
Code:
find ./ -maxdepth 1 -name "***"
For more details, please read the manpages.
 
Old 03-30-2009, 01:31 AM   #6
Techno Guy
Member
 
Registered: Dec 2008
Posts: 47

Original Poster
Rep: Reputation: 15
Ah great, thanks!
 
Old 03-30-2009, 01:52 AM   #7
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,049

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
If this is for anything serious I'd consider using inotifywatch/inotifywait instead. That way:
  • you don't need to save anything in an external file to keep track of what's new, what's old
  • you don't have to check periodically
  • new dirs are detected instantly
 
Old 03-30-2009, 02:24 AM   #8
Techno Guy
Member
 
Registered: Dec 2008
Posts: 47

Original Poster
Rep: Reputation: 15
Ah yea, thanks i92guboj.
That looks like a much better way, ill give it a go when im feeling better, kinda coming down with a cold or something
 
Old 03-31-2009, 01:26 AM   #9
Techno Guy
Member
 
Registered: Dec 2008
Posts: 47

Original Poster
Rep: Reputation: 15
Ok, I gave it a go now, something to keep my mind off my cold

So far I got:
Code:
#!/bin/bash

echo start

while inotifywait -e create ~/demo/; do

sleep 5

/whileloop.sh &

echo done

done
So that works great, so when I have the script running and I make a new folder in ~/demo/ I get "/root/demo/ CREATE,ISDIR testdemo" echoed back and it executes the whileloop.sh script just fine.

But I was wonder if I could somehow get the name of the new folder just created into a variable (eg. $newfolder)?
So that I can CD into the newly created folder.

Thanks for any past/further help

Edit:
Ok I changed it a bit:
Code:
while inotifywait --format %f -e create ~/demo/; do
so now the out put is just the folder name, but I still need to get that into a variable so I can CD into it...

Edit again:
Don't worry I got it working

Code:
while TEST=$(inotifywait --format %f -e create ~/demo/); do
echo New file or folder $TEST 

sleep 5

/whileloop.sh $TEST &
done
Please let me know if I done something wrong..?
But it seems to work.

Last edited by Techno Guy; 03-31-2009 at 03:19 AM.
 
Old 03-31-2009, 10:04 AM   #10
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,049

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
Code:
while TEST=$(inotifywait --format %f -e create ~/demo/); do
echo New file or folder $TEST 

sleep 5

/whileloop.sh $TEST &
done

You can make it slightly more elegant by feeding it into the loop itself like this:

Code:
inotifywait --monitor --format %f -e create tmp/ | while read file; do echo "$file"; done
Note that if you do it your way, it will exit after the creation of the first directory, which means that, potentially, your script won't be able to register if some other process creates two directory very near in time, between when the creation of the first dir is detected, and the time when the next instance of inotifywait has been spawned (after the previous one has been defunct), there'll be a race condition there.

On the contrary, inotifywait never is closed when you use --monitor, which means that once started it won't miss anything. It also saves some cpu hassle because there's no need to create and destroy the inotify process once and again, and again. It's just spawned once and it remains vigilant.

I also wonder why did you put the contents of the loop in an external file, I guess that even a function could have worked better there, though it depends on what are you doing.

Last edited by i92guboj; 03-31-2009 at 10:06 AM.
 
Old 04-03-2009, 10:54 PM   #11
Techno Guy
Member
 
Registered: Dec 2008
Posts: 47

Original Poster
Rep: Reputation: 15
Thanks again i92guboj,

Im using: (and works grate now! )
Code:
inotifywait --monitor --format %f -e moved_to -e create /demo/ | while read file; do echo "New files moved in $file";

/forloop.sh $file &

done
I have the forloop code in a external script so that I can run multiple "forloop.sh" scripts at once in the background since sometimes there will be several folders added at once and the forloop.sh can time a long time to complete at times (also the forloop.sh script has a bunch of other stuff in it too, not just a for loop.

But thanks again for you help.
 
Old 07-16-2009, 06:10 AM   #12
Techno Guy
Member
 
Registered: Dec 2008
Posts: 47

Original Poster
Rep: Reputation: 15
When using "--event create" it does detect both folders and files, but this is for a created file/folder, I want it for a moved file/folder.
inotifywait --monitor --format %f --event create /root #works with both files/folders


So then I use this, but using this only sees when a folder is moved in, when I move in a file nothing is shown...

inotifywait --monitor --format %f --event moved_to /root #Only works for folders

Any idea what's going on?

PS, Im using Linux, Debian 5 (Lenny).
 
Old 07-16-2009, 12:13 PM   #13
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,049

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
No idea, it should work, in any case I just tested and is seems to catch files as well as directories.
 
Old 07-16-2009, 08:41 PM   #14
Techno Guy
Member
 
Registered: Dec 2008
Posts: 47

Original Poster
Rep: Reputation: 15
What Linux distro are you using? (Debain...?)

Also if I do this:
inotifywait /root
And then move a file into /root I get "/root/ CREATE test.jpg", also then if I move a Folder to /root I get, "/root/ MOVED_TO,ISDIR FolderName" (it shows correctly).
 
Old 08-08-2009, 11:50 AM   #15
Hackbard23
LQ Newbie
 
Registered: Aug 2009
Posts: 1

Rep: Reputation: 0
Hi,
i recently came across this nice tool inotifywatch and it seems to do exactly what i want.
but unfortunately i can not get familiar with it´s arguments

what i want is to monitor a folder (and its subfolders) to trigger, when an sfv file is being created.
with that sfv file it should start cksfv -f /DIR/file.sfv >> /DIR/sfvcheck.txt.

how to do that with inotifywatch ?

help would be very much appreciated
 
  


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
bash check folder exists zerocool22 Programming 22 06-01-2008 08:53 AM
Bash script: Parse folders in path string miceagol Programming 3 03-25-2008 07:31 PM
bash script help - finding last n log files in all sub folders jeepescu Programming 4 11-03-2007 08:57 PM
bash script, for all files in folder do ............ Simon_6162 Programming 2 07-18-2007 03:40 PM
Cannot create folders with bash script called from php keyF Linux - Software 4 06-25-2006 11:58 AM


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

Main Menu
Advertisement
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