user3327194 user3327194 - 2 months ago 21
Bash Question

Copy files to my usb ONLY in shell script

I need to write a sh script that will copy the files from the hard drive to MY CONNECTED USB. The connected USB will be the same one always and I want to make sure that these non computer savvy people don't copy to another USB. This script needs to be copied to multiple LINUX computers that can have different number of partitions or a numer of USBs connected. Data protection is not important.

My first thought: simply copy the files to "MY USB FLASH DRIVES NAME". That way if an USB with another name gets connected, data will not be copied to it.

How to do this ?


first, plug your usb in and look at dmesg:

$ dmesg

and you will see something like:

[98937.147950] sd 15:0:0:0: [sdb] Attached SCSI removable disk

then go to /dev and gather the UUID of your pendrive:

$ ls /dev/disk/by-uuid/ -l


98A9-D422 -> ../../sdb1

now you can make a script like this:

mkdir /mnt/my_pendrive
mount /dev/disk/by-uuid/98A9-D422 /mnt/my_pendrive


And it will work in every computer, as UUID will be the same anywhere you plug your pendrive in

Whenever you want to check whether is precisely that pendrive which has been connected, just check the /dev/disk/by-uuid/ directory to see if 98A9-D422 exists.