3

I would like to automatically copy the contents of a USB flash drive to the internal storage of the Pi whenever I insert the flash drive into one of the Pi's USB ports.

I have set up a python script to handle the copying:

import os
from shutil import copyfile, ignore_patterns

source = '/media/pi/4DD7-393F/'
files = os.listdir(source)

dest = '/home/pi/Desktop/slideshow/photo_frames/daniel/images/'

for file in files:
    if file.endswith(('ANI', 'BMP', 'CAL', 'FAX', 'GIF', 'IMG', 'JBG', 'JPE', 'JPEG', 'JPG',
   'jpg', 'MAC', 'PBM', 'PCD', 'PCX', 'PCT', 'PGM', 'PNG', 'PPM', 'PSD', 'RAS','TGA', 'TIFF', 'WMF')):
    copyfile(source + file, dest + file)

When run from terminal with python /home/pi/Desktop/slideshow/photo_frames/daniel/usb_copy.py the script functions as expected - files with the corresponding extensions are copied into the appropriate destination folder.

In an attempt to automate this process upon my inserting the flash drive, I tried creating a udev rule. To do so, I obtained identifying information for my flash drive using udevadm info /dev/sda1 and received the following:

P: /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host8/target8:0:0/8:0:0:0/block/sda/sda1
N: sda1
S: disk/by-id/usb-Kingston_DataTraveler_2.0_50E549C20280EE61B98F0011-0:0-part1
S: disk/by-path/platform-3f980000.usb-usb-0:1.4:1.0-scsi-0:0:0:0-part1
S: disk/by-uuid/4DD7-393F
E: DEVLINKS=/dev/disk/by-id/usb-Kingston_DataTraveler_2.0_50E549C20280EE61B98F0011-0:0-part1 /dev/disk/by-path/platform-3f980000.usb-usb-0:1.4:1.0-scsi-0:0:0:0-part1 /dev/disk/by-uuid/4DD7-393F
E: DEVNAME=/dev/sda1
E: DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host8/target8:0:0/8:0:0:0/block/sda/sda1
E: DEVTYPE=partition
E: ID_BUS=usb
E: ID_DRIVE_THUMB=1
E: ID_FS_TYPE=vfat
E: ID_FS_USAGE=filesystem
E: ID_FS_UUID=4DD7-393F
E: ID_FS_UUID_ENC=4DD7-393F
E: ID_FS_VERSION=FAT32
E: ID_INSTANCE=0:0
E: ID_MODEL=DataTraveler_2.0
E: ID_MODEL_ENC=DataTraveler\x202.0
E: ID_MODEL_ID=6545
E: ID_PART_ENTRY_DISK=8:0
E: ID_PART_ENTRY_NUMBER=1
E: ID_PART_ENTRY_OFFSET=2
E: ID_PART_ENTRY_SCHEME=dos
E: ID_PART_ENTRY_SIZE=60978814
E: ID_PART_ENTRY_TYPE=0xc
E: ID_PART_TABLE_TYPE=dos
E: ID_PATH=platform-3f980000.usb-usb-0:1.4:1.0-scsi-0:0:0:0
E: ID_PATH_TAG=platform-3f980000_usb-usb-0_1_4_1_0-scsi-0_0_0_0
E: ID_REVISION=PMAP
E: ID_SERIAL=Kingston_DataTraveler_2.0_50E549C20280EE61B98F0011-0:0
E: ID_SERIAL_SHORT=50E549C20280EE61B98F0011
E: ID_TYPE=disk
E: ID_USB_DRIVER=usb-storage
E: ID_USB_INTERFACES=:080650:
E: ID_USB_INTERFACE_NUM=00
E: ID_VENDOR=Kingston
E: ID_VENDOR_ENC=Kingston
E: ID_VENDOR_ID=0930
E: MAJOR=8
E: MINOR=1
E: SUBSYSTEM=block
E: TAGS=:systemd:
E: USEC_INITIALIZED=18625356

In my /etc/udev/rules.d/ folder, I have created a file, 100-mydevice.rules and it contains:

ACTION=="add", ATTRS{idVendor}=="0930", ATTRS{idProduct}=="6545" RUN+="python /home/pi/Desktop/slideshow/photo_frames/daniel/usb_copy.py"

When I insert the flash drive, the script does not run - at least, the files are not copied properly. Any information regarding how to fix this issue would be greatly appreciated.

1 Answer 1

1

Try running sudo udevadm trigger --verbose and sudo udevadm control --reload-rules. Also, make sure you have rebooted the machine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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