Script to monitor folder for new files?
You should consider using inotifywait
, as an example:
inotifywait -m /path -e create -e moved_to |
while read dir action file; do
echo "The file '$file' appeared in directory '$dir' via '$action'"
# do something with the file
done
In Ubuntu inotifywait
is provided by the inotify-tools
package. As of version 3.13 (current in Ubuntu 12.04) inotifywait
will include the filename without the -f option. Older versions may need to be coerced. What is important to note is that the -e
option to inotifywait
is the best way to do event filtering. Also, your read
command can assign the positional output into multiple variables that you can choose to use or ignore. There is no need to use grep/sed/awk to preprocess the output.
You can use watch
in your script
watch -n 0.1 ls <your_folder>
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, watch
only supports minimum of 0.1 seconds.
I just cooked up this, and see no huge problems with it, other than a tiny chance of missing files in between checks.
while true
do
touch ./lastwatch
sleep 10
find /YOUR/WATCH/PATH -cnewer ./lastwatch -exec SOMECOMMAND {} \;
done
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.