2

I'm not a pro in shell scripting, thats why I ask here :).

Let's say I got a folder. I need a script that monitors that folder for new files (no prefix name of files is given). When a new file gets copied into that folder, another script should start. Has the second script processed the file successfully the file should be deleted.

I hope you can give me some ideas on how to achieve such script :)

Thank you very much in advance. Thomas

user2428207
  • 825
  • 4
  • 16
  • 29
  • http://stackoverflow.com/questions/7566569/how-to-continuosly-monitor-the-directory-using-dnotify-inotify-command – devnull Aug 16 '13 at 06:56

2 Answers2

5

Try this:

watcher.sh:

#!/bin/bash
if [ -z $1 ];
then
    echo "You need to specify a dir as argument."
    echo "Usage:"
    echo "$0 <dir>"
    exit 1
fi

while true;
do
    for a in $(ls -1 $1/* 2>/dev/null);
    do
        otherscript $a && rm $a #calls otherscript with the file a as argument and removes it if otherscript returned something non-zero
    done
    sleep 2s
done

Don't forget to make it executable

chmod +x ./watcher.sh

call it with:

./watcher.sh <dirname>
davvs
  • 1,029
  • 1
  • 11
  • 18
1

try inotify(http://man7.org/linux/man-pages/man7/inotify.7.html)

or you may need to install inotify-tools (http://www.ibm.com/developerworks/linux/library/l-ubuntu-inotify/) to use it by shell.

tesla
  • 11
  • 1