Automatically transfer all data from USB

Hello Everybody,

Today I have done some something unusual. I write some script in Linux. My ultimate goal was trying to do copy all file from flash drive automatically when it is plugged into my computer. To achieve my goal I need to learn some new things 🙂

First I have to learn how to triggered my bash script when USB plugged in o computer. In Linux, one way to do this is udev rules. I wrote my own rule which any USB plugged into my computer will triggered my batch script that copy all the files from USB to my computer.

Here is very useful documentation for how to write udev rules. Here is my rule which it is specifically about my flash drive.

KERNEL=="sd*", ATTRS{idVendor}=="1d6b", ACTION=="add", SYMLINK+="NewUSB", RUN+="/home/username/auto_relay.sh"

auto_realy.sh script.

#!/bin/bash
/home/username/auto_import.sh & exit

auto_copy.sh

#!/bin/bash
sleep 5;
cd /home/username;
mkdir mountFolder;
mount /dev/NewUSB mountFolder;
mkdir fromFlash;
sleep 3;
cp -a mountFolder/* fromFlash/;
chown username:username -R fromFlash/;

You may want to mount different place and randomly create a new folder name for each flash drive plugged into computer.

This script and udev rule can be improved.

Some useful command;

sudo udevadm monitor

sudo udevadm info

sudo udevd –debug   // this command show all the things when USB plugged and unplugged.

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s