I have a script set up to run when a certain usb drive is inserted. It is supposed to copy a folder from the usb drive into a backup folder, and write over this folder if it already exists. However, when I plug in my usb drive, it creates the folder but it doesn't copy any files.

My .rules looks like this:

ACTION=="add", ATTR{idVendor}==Vendor, ATTR{idProduct}==Product, RUN+="/bin/sh /path/to/file/mkBackup"

My file mkBackup looks like this:

if [ -e $a ]; then
        rm -rf $a
mkdir $a
cp -r /path/to/usb/folder $a

When it is run by udev, the Backup folder is created but it is empty, but when I just run ./mkBackup, the folder is created and everything is copied. This is my first time using udev to do something like this, so please forgive me if this seems like a simple question. Thank you in advance.

1 Answer 1


Given that the Backup folder is created, you know the script successfully executes when you plugin the device. I suspect that the script is executing before the device actually finishes mounting. Thus the files are not yet available for cp.

What happens if you add sleep 5 after mkdir $a ? Or what if you try to copy existing files instead?

This link is also pretty helpful as a reference.

I also use:

udevadm monitor --udev

To watch udev events in real time.

udevadm test /path/to/dev

To simulate a udev event run for the device. Note that this does not run any program specified by a RUN key.

EDIT: As Gilles helpfully points out, my idea won't work. And I'm not aware of a solution. Here's why:

Udev will make the USB device available to the OS. Then, the OS will realize that the OS contains a mountable file system and will mount it. At that point the files on the USB device are actually available.

Any script run by a udev rule will execute before the file system is actually mounted. If I think of an alternative I'll update the post.

  • Try this in your udev-rule RUN+="/bin/sh -c /path/to/file/mkBackup"
    – user192526
    Nov 2, 2016 at 7:52
  • Adding the sleep line doesn't seem to help. It makes it so the file manager doesn't open the window that opens when you normally plug in a usb drive (running ubuntu) on a delay. I know it can copy over files though because it copies properly when I run the script manually.
    – acn3
    Nov 2, 2016 at 20:19
  • @Bahamut This doesn't work either.
    – acn3
    Nov 2, 2016 at 20:19
  • This is not going to help. Udev waits for the script to finish before it finishes its own job. The job of udev is to make the disk device available to the system. Until the disk device is available, a filesystem on the disk can't be mounted. Nov 2, 2016 at 22:15
  • You might be able to work around the problem @Gilles pointed out by spawning a subprocess to wait before doing the backup -- something like (sleep 5; cp -r /path/to/usb/folder $a) &. BTW, I'd recommend using rsync -a --delete instead of deleting & recopying the folder. That way if something goes wrong, it won't have just auto-deleted your last good backup. Nov 3, 2016 at 6:34

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.