First, I should note that I am pretty new to writing scripts, but I know my way around ubuntu well enough that I don't mind getting my hands dirty...

I have a small script, which does a simple directory watch and when it finds a new file in the directory, copies that file to another directory. The script I am using looks like this:

sudo rsync -av --omit-dir-times --delete --progress /home/sunmorgus/tempx/Complete/ /media/complete/

while [ 1 ]
  echo 'Watching directory: '$WATCHED_DIR 'for new files'
  while file=$(inotifywait -q -e create "$WATCHED_DIR" --format "%f")
    echo 'New file to upload to moonback' $file
    notify-send -i "gtk-go-up" "Complete Folder Monitor" "Uploading file $file"

    #rsync goes here
    sudo rsync -av --omit-dir-times --delete --progress /home/sunmorgus/tempx/Complete/ /media/complete/

    notify-send -i "gtk-home" "Complete Folder Monitor" "File uploaded."

It's a modification of a script I found here: http://www.webupd8.org/2010/11/script-to-watch-folder-and-upload-new.html

I have a few questions about the script. First off...is this the best way to do what I am trying to do? Like I said, it works fine, but I'm always open to new ideas. Note: the destination directory is an SMB share on a Windows 7 machine.

Second...what would be the best way to run this script at startup? Right now, I went into the "Startup Applications" screen and added a new record that runs this line "gnome-terminal -e /script.sh" which works, but doesn't seem ideal. I tried adding the script to the init.d folder and running update-rc.d, and adding the script to /etc/rc.local but neither of those worked (I got no errors, but the script wasn't running after a reboot).

Lastly...is there a way to modify this script so that it also deletes from the destination when deleted from the source? The script only runs when files are added, not deleted, and while the rsync portion will delete from the destination when deleted from the source, it won't do it until a new file is added. (hopefully that paragraph makes sense).

2 Answers 2


For your questions...

  1. This will work reasonably well. If you want to get fancy, look into famd.
  2. Add it to the end of /etc/rc.local or create proper init scripts according to your distro's standard method.
  3. The --delete option to rsync should do that already.
  • 2. I tried adding it to the rc.local...it didn't work. I could try again i suppose... 3. I already have the --delete, but the inotify part of the script doesn't recognized deletes, so it will only delete files when a new file is added. I need to know how to make inotify recognized adds and deletes... Jul 29, 2011 at 17:56
  • @sunmorgus Then you'll have to create proper init scripts. The particulars will depend on which distro you're using. I'm not sure what to do about inotify. Maybe you'll want to switch to fam anyway.
    – bahamat
    Jul 29, 2011 at 19:05

I would do something like this:

cd directory2watch
# watching SUBdirectories
inotifywait --monitor --recursive *|
        while read path action file
                # To get an idea of possible actions that will be
                # reported:
                echo path=$path action=$action file=$file
                # Decide on a followup base on $action:
                case $action in
                  "DELETE") ...

  • It's possible that a double-quote is missing on the end of the CLOSE_WRITE,CLOSE line; you can edit your post if one should be there.
    – Ben N
    Mar 27, 2017 at 23:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .