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 -> ../../sdb
lrwxrwxrwx. 1 root root 10 Mar 12 01:01 /dev/disk/by-id/usb-JMTek_USBDrive-0:0-part1 -> ../../sdb1
With 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/sdb1
And 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/HOLA
Getting a device's ID
You 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_USBDrive
JMTek_USBDrive
Incidentally 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= 0
D: Ver= 1.10 Cls=00(>ifc ) Sub=00 Prot=00 MxPS= 8 #Cfgs= 1
P: Vendor=058f ProdID=9380 Rev=01.00
S: Manufacturer=JMTek
S: Product=USBDrive
C: #Ifs= 1 Cfg#= 1 Atr=80 MxPwr=100mA
I: 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_SERIAL"
E: ID_MODEL=USBDrive
E: ID_SERIAL=JMTek_USBDrive-0:0
E: ID_VENDOR=JMTek