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:

fdisk -l lists all the partitions on all connected drives, including a summary of the drive parameters . EG:

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.

Leave a Reply