How do I detect new files in a folder with a bash script? I would like to process the files as soon as they are created in the folder. Is this possible to do so or do I have to schedule a script with cron that that check for new files each minute or so?
|
|
You should consider using
In Ubuntu |
|||||||||||||||||||||
|
|
I prefer Ex:
You can see a full example here: http://www.cyberciti.biz/faq/linux-inotify-examples-to-replicate-directories/ |
||||
|
|
|
I just cooked up this, and see no huge problems with it, other than a tiny chance of missing files in between checks.
If your file processing doesn't take too long, you should not miss any new file. You could also background the activities... It's not bullet proof, but it serves some purposes without external tools like inotify. |
|||||||||||||||||||||
|
|
I am assuming the target folder (I'll call it You can use the following command:
just to check if the folder is still empty, in fact it will return a 0 if there is no new file (hence the That said a silly if/then test can make the rest of the work:
Of course the Adding a line like the following in your crontab will run the check once a minute and will trigger the
|
|||||||||
|
|
If you want to detect new files, then process them and at the end delete proceeded files you can use systemd.path. This method bases on inotify. There is an option DirectoryNotEmpty, so systemd can run your script always when it detects any files in directory. You have to remember it will work only if you can delete proceeded files and script leaves directory empty. First prepare mymonitor.service file
next go to mymonitor.path to define the path
If the name of the .path file is the same as the name of the service there is no need to specify the service name in .path file. It bases on Monitoring File Access for Dummies |
|||
|
|
|
Bash cannot do this easily. You'd have to basically get a list of all the files in the folder and periodically get a new list and compare them to see whats changed. What you're looking for is called inotify. Its built into the linux kernel and you can basically sit there waiting for something to happen at which point inotify comes back and says 'hey, theres a new file called foobar' To accomplish what you want you'd have to switch to something like perl and use Linux::Inotify2 (python probably supports inotify as well, but I'm a perl person). |
|||
|
|
|
This works in cygwin and Linux. Some of the previous solutions which write a file will cause the disk to thrash. This scipt does not have that problem:
|
||||
|
|
|
Below is an abridged version of example on stackoverflow that I've tested and incorporated into one of my projects that requires monitoring of specific directories.
Here's a link to a script that uses a modified version of above to automatically decrypt files or directories found in its sshfs mount point; the afore mentioned project. |
||||
|
|
|
You can use
Monitors your folder and lists you everything in it every 0.1 seconds Drawback Is not real time, so if a file was created and deleted in less than 0.1 second, then this would not work, |
|||
|
|