bash script that will check folder x for new folders.
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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...)
#!/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?
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:
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 02:19 AM.
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.
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.
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
Also if I do this:
inotifywait /root
And then move a fileinto /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).
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.