Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)?

share|improve this question
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
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/"
ACTION=="remove", ATTRS{idVendor}=="09da", ATTRS{idProduct}=="0260", OWNER="{userid}", RUN+="/usr/local/bin/"

The usb-copy scripts could be done in multiple ways. They will look something like 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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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