Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm looking for a way to plug my flash drive into a computer and have it automatically copy all the files from a specific folder when it's plugged in? My son is being secretive about a party he went to, and I know he has pictures in his My Documents folder, but he won't let me see them. Is this possible? And if so, can it be made to run in the background so there is no evidence of the copying?

share|improve this question
    
"My Documents"? Is it Microsoft Windows? This site is about Ubuntu. –  Eric Carvalho Jun 18 '13 at 2:02
    
My apologies- I clicked on the wrong link –  Brenden Jun 18 '13 at 2:29
add comment

1 Answer

On Linux, it is generally not possible to program a flash drive to perform an action when it is inserted the way Windows does with autostart.bat. You can however, program the computer to perform an action when a specific usb drive is plugged in by creating udev rules. This requires root access, though, which would render your difficulties accessing your son's photos moot.

The fact that your son stores his photos in his My Documents folder makes me suspect that his computer is running Windows. If that is the case, you might want to ask your question over at Super User.

If not here's ...

a quick guide to using udev rules to perform an action when a flash drive is inserted.

  1. We need some information about your flash drive, so plug it in and in a terminal, type

    cat /proc/scsi/usb-storage/*
    

    and find the entry that had your flash drive's vendor and product. Copy down the serial number for the flash drive--we'll need it later.

  2. Now, we'll write a rule for udev to follow when the flash drive is inserted. Again, in the terminal:

    sudo nano /etc/udev/rules.d/99.superspy.rules
    

    This will open up an editor and create a file in the /etc/udev/rules.d directory called 99.superspy.rules. This file can be called whatever you'd like, but you should know that udev will process rules in alphabetical order, so a rule that starts with '99' will be processed after a rule that starts with '10'.

  3. In the editor add the following line to your file (which should be blank at this time):

    KERNEL=="sd*1", ACTION=="add", ATTRS{serial}=="your serial number here"", SYMLINK="spy_drive", RUN+="/usr/local/bin/my-spy-script.sh /dev/spy_drive"

    This should all be on one line. What this rule does is tell your computer to create a symbolic link at /dev/spy_drive that points the the drive and then run the script /usr/local/bin/my-spy-script.sh.

  4. Create your super sneaky script. Type the following in the terminal:

    sudo nano /usr/local/bin/my-spy-script.sh
    
  5. Create your nefarious script. Here's an outline to help you out:

    #!/bin/sh
    # Get the device name
    DEVICE=$1
    # Exit if script is run without a device name
    if [ ! -e ${DEVICE} ] ; then exit 1; fi
    
    # Let the device settle
    sleep 10
    
    # Mount the device
    mkdir /mnt/spy-drive
    mount ${DEVICE} /mnt/spy-drive 
    
    # Perform your copying
    cp -R "/home/yourson/My Documents/Party Photos/*" /mnt/spy-drive
    
    # Clean up
    umount ${DEVICE}
    rm -rf /mnt/spy-drive
    

Notes:

There are some dangers and notes associated with this method that you should be aware of: - If the user removes the drive during the copy operation, then the flash drive's filesystem may very well become corrupted. So it might be good to use a flash drive that's got a blinky light that can tell you when it's not being copied to. You might also modify the script so that the computer plays a sound.

  • The script outline given above is very slapdash. It doesn't do much checking to make sure that things are actually the way it expects them to be. Particularly, it doesn't check to see if anything is already mounted at /mnt/spy-drive. If there is something there, it won't be for long.

  • This is an overblown solution for trying to access photos on the computer. As I previously mentioned, in order to implement this you need to have root access, which means you could just copy the photos anyway. However, this method could come in handy if you tweak the script a little bit to provide an easy way to back up data. I'm sure you can think of other ways to modify the script to perform useful tasks.

share|improve this answer
add comment

Your Answer

 
discard

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.