How would I return (output) the path to any USB flash memory stick(s) connected to the local computer using bash (Ubuntu and Linux Mint)?Background:I'm providing users with an automated backup script. (The actual backup software is already installed on their computer.) The user's job is to plug in a USB flash memory stick and enter one command at the terminal (without any parameters, options or any other variable information).I need a bash script that can find the path to the USB flash memory stick. If more than one such path is found, I will probably just abort and pop up a message to contact me. Rather than make a complicated script, it is easier for me to just tell them to make sure only one memory stick is plugged into the computer at the time they wish to perform a backup.
Bash script to output path to USB flash memory stick
bash;shell script;filesystems;path;usb drive
After plugging in a USB device you can tell what was installed by simply looking at this path:$ ls -l /dev/disk/by-id/usb*Example$ ls -l /dev/disk/by-id/usb*lrwxrwxrwx. 1 root root  9 Mar 12 01:01 /dev/disk/by-id/usb-JMTek_USBDrive-0:0 -> ../../sdblrwxrwxrwx. 1 root root 10 Mar 12 01:01 /dev/disk/by-id/usb-JMTek_USBDrive-0:0-part1 -> ../../sdb1With the above information your script could simply look at those entries using something like readlink:$ readlink -f /dev/disk/by-id/usb-JMTek_USBDrive-0:0*/dev/sdb/dev/sdb1And then using the mount command walk backwards to find out what directory the device was automounted under:$ mount | grep '/dev/sdb\b'$ mount | grep '/dev/sdb1\b'/dev/sdb1 on /run/media/saml/HOLA type vfat (rw,nosuid,nodev,relatime,uid=1000,gid=1000,fmask=0022,dmask=0077,codepage=437,iocharset=ascii,shortname=mixed,showexec,utf8,flush,errors=remount-ro,uhelper=udisks2)This could be expanded to a one liner like this:$ readlink -f /dev/disk/by-id/usb-JMTek_USBDrive-0:0* | \    while read dev;do mount | grep $dev\b | awk '{print $3}';done/run/media/saml/HOLAGetting a device's IDYou can parse this out like so from the output from /dev/disk/by-id/usb*, like so:$ ls /dev/disk/by-id/usb* | sed 's/.*usb-\(.*\)-[0-9]:.*/\1/'JMTek_USBDriveJMTek_USBDriveIncidentally this information is a concatenation of the USB's manufacturer + product descriptions.$ usb-devices...T:  Bus=02 Lev=02 Prnt=02 Port=01 Cnt=02 Dev#= 10 Spd=12  MxCh= 0D:  Ver= 1.10 Cls=00(>ifc ) Sub=00 Prot=00 MxPS= 8 #Cfgs=  1P:  Vendor=058f ProdID=9380 Rev=01.00S:  Manufacturer=JMTekS:  Product=USBDriveC:  #Ifs= 1 Cfg#= 1 Atr=80 MxPwr=100mAI:  If#= 0 Alt= 0 #EPs= 2 Cls=08(stor.) Sub=06 Prot=50 Driver=usb-storage...You can also access it this way once you've established which device (/dev/sd*) the USB device is using, through UDEV:$ udevadm info --query=all --name=sdb | grep -E MODEL=|VENDOR=|ID_SERIALE: ID_MODEL=USBDriveE: ID_SERIAL=JMTek_USBDrive-0:0E: ID_VENDOR=JMTek