Linux Autocopy files from Removable media
Autocopy files

Disclaimer: I am running Lubuntu 12.04, and tested the script on Ubuntu 10.04LTS too. I relied on a modification to the udev rule that is posted in this post ( drive automounting.

I needed to have a script run to move video files from a pendrive to a local drive.
I wanted to just plug it in, and let it do it's thing without intervention from me. Pendrives are not the fastest devices out there so and copying several gigs can take a while.
Using the XBMC FileManager does tie up the XBMC gui while the copying/moving is processed.

I came up with the following script:

#    Script to copy files from removable media under
#    /moviecopy folder to
#    /movie on local drive
#     2012-08-27 pkarrasquillo
#     v1.0

/bin/sleep 5

if [ "$(ls -A $1/moviecopy/*)" ]
cp -rfu $1/moviecopy/* $2/Movies
rm -rf $1/moviecopy/*

if [ "$(ls -A $1/tvcopy/*)" ]
cp -rfu $1/tvcopy/* $2/TVShows
rm -rf $1/tvcopy/*

if [ "$(ls -A $1/videocopy/*)" ]
cp -rfu $1/videocopy/* $2/Videos
rm -rf $1/videocopy/*

rm -rf $1/diskfree*
df -kh > $1/diskfree-$(date '+%m%d%y-%H-%M-%S').txt

/bin/beep && /bin/beep
curl 'http://localhost:8080/xbmcCmds/xbmcHttp?command=ExecBuiltIn&parameter=XBMC.updatelibrary(video)'
exit 0

And a caller script:

/home/xbmc/Script/ $1 $2 &
exit 0

To install, on the Lubuntu Command line, as normal user that runs xbmc:

cd $HOME
mkdir Script
cd Script


{cut and paste the script in the nano window. Then, ctrl-o to save, ctrl-x to exit}

I called it "", and placed it under $HOME/Script folder I created. Then I made it executable:

chmod +x

Do the same steps with the second script, call it

From the "Howto Install XBMC PVR Xvba for AMD/ATI Radeon and Fusion GPUs" thread:
auto mount external drives/usb sticks. 'xbmc' is the user running xbmc. Change as needed.

sudo adduser xbmc users

create or edit the file


as root (sudo) and modify this line

# Start at sdb to avoid system harddrive.
KERNEL!="sd[b-z][0-9]", GOTO="media_by_label_auto_mount_end"

# Import FS infos
IMPORT{program}="/sbin/blkid -o udev -p %N"

# Get a label if present, otherwise specify one
ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}"
ENV{ID_FS_LABEL}=="", ENV{dir_name}="usbhd-%k"

# Global mount options
ACTION=="add", ENV{mount_options}="relatime"
# Filesystem-specific mount options
ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,gid=100,umask=002"

# Mount the device
#    ...Comented original entry:
#ACTION=="add", RUN+="/bin/mkdir -p /media/%E{dir_name}", RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/%E{dir_name}"
#     ... My mod to call
ACTION=="add", RUN+="/bin/mkdir -p /media/%E{dir_name}", RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/%E{dir_name}", RUN+="/home/xbmc/Script/ /media/%E{dir_name} /media/Pedro-WD &"

# Clean up after removal
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l /media/%E{dir_name}", RUN+="/bin/rmdir /media/%E{dir_name}"

# Exit


Note that on the RUN+= line we appended to the end you can specify your own destination directory:

# Mount the device
# ...Comented original entry:
#ACTION=="add", RUN+="/bin/mkdir -p /media/%E{dir_name}", RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/%E{dir_name}"
# ... My mod to call
ACTION=="add", RUN+="/bin/mkdir -p /media/%E{dir_name}", RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/%E{dir_name}", RUN+="/home/xbmc/Script/ /media/%E{dir_name} /media/Pedro-WD &"

RUN+="/home/xbmc/Script/ /media/%E{dir_name} /media/Pedro-WD"
RUN+="{....script path..... } {....copy-from mount path....} {.....copy-to destination path....}"


reboot or reload udev rules


udevadmin control --reload-rules


on your usb flash drive root folder, create folders called


You can have those same folder names in any number of removable drives you have. The udev rule will pick up on it and will move the contents to the local drive as well.

EDIT: I run a 'caller' script on the udev rule now, instead of the main script. Udev will timeout and kill the main script if you are copying a large amount of data. So instead, I call the 'caller' script, and from there try to fork a new thread for instead. That way if udev decides to kill, should still keep alive and finish what it does.

The main script will move all files and folder under these and when done, will attempt to 'beep' the computer speaker. If you do not have the beep package, it will just exit and throw the error messages to the log, no harm done. I never got beep to work in my gear though.

The script will also dump a report of filesystem disk space usage on the removable media on a file named 'diskfree-%date-%time.txt'. That way you can keep track of your filesystem's usage.

Lastly, at the very end, it will attempt to send a command to XBMC to update the library. You will need the curl command (sudo apt-get install curl). You can make it work via wget, but its messier. It will not work if XBMC's webserver is not running. If you changed the XBMC's webserver port, then this last part needs to be modified accordingly.

One thing I still need to work on is to correct the removable media getting installed as root. This becomes a problem if your xbmc install has a desktop environment underneath. The gui file manager (pcmanfm) runs as user, so it will not be able to mount/unmount/eject the pendrive for you unless you launch it as sudo. But it is not a problem if you pretty much do XBMC fullscreen all the time and seldom do filemanagement from the desktop gui.

mods, move this to tips and tricks section if appropiate

Logout Mark Read Team Forum Stats Members Help
Autocopy files from Removable media2
This forum uses Lukasz Tkacz MyBB addons.