Finding the Linux device name of a USB Drive
I had a scripting problem due to the fact that USB drives plugged into the server did not always get the same device name. Sometimes they got /dev/sdb and sometimes /dev/sdc.
I used this command to determine what the device name was so I could then mount it:
1 |
fdisk -l|grep 'Disk\ /dev/sdb\|Disk\ /dev/sdc'|cut -c6-13 |
fdisk -l lists all the partitions on all connected drives, including a summary of the drive parameters . EG:
1 2 3 4 5 6 |
Disk /dev/sdc: 319.4 GB, 319370035200 bytes 255 heads, 63 sectors/track, 38827 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00035f28 |
The grep 'Disk\ /dev/sdb\|Disk\ /dev/sdc' command filters out everything except for the first line of the summary for the /dev/sdb or /dev/sdc drive. IE either:
Disk /dev/sdb: 319.4 GB, 319370035200 bytes
or
Disk /dev/sdc: 319.4 GB, 319370035200 bytes
(I couldn’t grep for just /dev/sdb or /dev/sdc because these strings appear again, once for each partition on the drive.)
Finally, the cut -c6-13 command cuts characters 6 through to 13 inclusive from the grep’d string. The result is either /dev/sdb or /dev/sdc.