I am trying to sync between a folder and usb drive, so that

a) when I plug in a particular usb drive, a script runs to copy any newer files from the usb to the folder; and

b) when I unmount the drive (click eject in nautilus), a script runs to copy any newer files from the folder to the usb.

I am confident that I can use udev and rsync to accomplish part a), but how can I achieve part b)?

  • 1
    possible duplicate of Autorun a script after I plugged or unplugged an USB device – Radu Rădeanu Aug 14 '13 at 15:03
  • @radu I dont like the answer given >:-D With the upstart it is probably possible too: you can create events on creation and deletion of files (and an umount removes a directory :) ) If I got time Ill add it to that one ;) – Rinzwind Aug 14 '13 at 15:07
  • @Rinzwind I don't like as well, but what can I do... The question is the same. – Radu Rădeanu Aug 14 '13 at 15:13
  • 1
    It's similar, but I'm not sure that it's the same. I'm looking to run a script when a particular usb is ejected. The other question is dealing with plugging and unplugging in general. – rudivonstaden Aug 14 '13 at 16:20

The duplicate answer has most of what's necessary. Comments welcome. Plug it in to get the ids:


replace the ids and tell it what scripts.

ACTION=="add", ATTRS{idVendor}=="09da", ATTRS{idProduct}=="0260", OWNER="{userid}", RUN+="/usr/local/bin/usb-copy-add.sh"
ACTION=="remove", ATTRS{idVendor}=="09da", ATTRS{idProduct}=="0260", OWNER="{userid}", RUN+="/usr/local/bin/usb-copy-remove.sh"

The usb-copy scripts could be done in multiple ways. They will look something like the following. This is the remove. Reverse things for the add (does not work recursively):

for x in `ls -1 "$localf"`
    file=`basename $x`
    cd {mounted dir}
    if [ "$file" -nt "$x" ]
        cp "$file" "$localf"

or from superuser answer there is a link that describes cp --update (with -r recursive). This is the remove, reverse for the add:

cp -r -u {mounted dir} {/your/local/folder/}

also from the same superuser answer, this is the remove, reverse for the add:

rsync --progress -r -u {mounted dir} {/your/local/folder/}

Hare are some other copy ideas.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.