1. Overview

Sometimes, we have multiple USB storage devices plugged into our machine. If we heavily use the command line, then we might need to identify the devices in order to carry out copy operations.

In this article, we’ll cover the many ways we can use to check for the block USB storage devices attached to our system. We’ll begin with manually identifying the device by checking the system logs. Next, we’ll cover the UNIX utilities that we can use for the same purpose.

Some of these utilities might not ship with our distributions, but we can install them from our package repository using a package manager. However, on modern popular distributions, there should be at least one tool available that we can use to identify our USB devices.

2. dmesg

dmesg is a tool that lets us check the device logs generated by device drivers.

Once we’ve plugged in our USB device, we’ll tail the logs produced by dmesg:

$ dmesg | tail -n 20

The output produced will look similar to this:

...
[10526.331968] sd 6:0:0:0: [sdb] 61472960 512-byte logical blocks: (31.5 GB/29.3 GiB)
[10526.332209] sd 6:0:0:0: [sdb] Write Protect is off
[10526.332216] sd 6:0:0:0: [sdb] Mode Sense: 03 00 00 00
[10526.332434] sd 6:0:0:0: [sdb] No Caching mode page found
[10526.332440] sd 6:0:0:0: [sdb] Assuming drive cache: write through
[10526.365116] sdb: sdb1
[10526.366565] sd 6:0:0:0: [sdb] Attached SCSI removable disk

Here in the output, we can see that our system successfully detected our USB device. In the second-to-last line, our device is now identified as sdb.

Let’s verify it:

$ find /dev -iname "sdb*"
/dev/sdb1
/dev/sdb

3. mount

The mount command lets us mount file systems and block devices. It ships with the majority of Linux distributions.

In the case of modern Linux distributions, the block devices are automatically mounted. So, assuming our USB drive is already mounted, we can quickly see the list of mounted devices by simply typing mount:

$ mount

However, we’re only interested in the devices mapped from the /dev directory. Conventionally, block USB device names in Linux start with the prefix sd followed by a unique letter. Therefore, we’ll narrow down our output to just that:

$ mount | grep -i /dev/sd
/dev/sda2 on / type ext4 (rw,relatime)
/dev/sdb1 on /mnt type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,utf8,errors=remount-ro)

As we can see in the output, /dev/sdb1, which is mounted on /mnt, is the USB device.

4. udev

udev is a device manager for the Linux kernel.

As long as udev is running on our system, we can identify our USB devices in the /dev/disk/by-id directory:

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

Once we execute the command, we can see our storage virtual files renamed in the form of manufacturer_serial:

lrwxrwxrwx 1 root root  9 May 25 20:30 usb-AI_Mass_Storage-0:0 -> ../../sdb
lrwxrwxrwx 1 root root 10 May 25 20:30 usb-AI_Mass_Storage-0:0-part1 -> ../../sdb1

5. udevadm

udevadm is a device management tool that manages the device’s events.

As an alternative to manually checking for attached devices under /dev/disk, we can use udevadm to print a lot of information for each drive:

$ udevadm info --query=all /dev/sd*
  • the info subcommand prints information for a device
  • –query=all will print all the related fields
  • /dev/sd* is the path to the device we want to print the information about

udevadm expects a path to the device, but we used the wildcard to print the information for the sdX devices.

Let’s see the output:

...
P: /devices/pci0000:00/0000:00:14.0/usb3/3-1/3-1:1.0/host6/target6:0:0/6:0:0:0/block/sdb
N: sdb
L: 0
S: disk/by-id/usb-AI_Mass_Storage-0:0
S: disk/by-path/pci-0000:00:14.0-usb-0:1:1.0-scsi-0:0:0:0
E: DEVPATH=/devices/pci0000:00/0000:00:14.0/usb3/3-1
...

6. lsblk

lbslk is a handy utility that we can use to list all the block devices. Besides the device name, it also prints extra information telling us whether the disk is mounted and the amount of free space available on the disk:

$ lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda      8:0    0 298.1G  0 disk 
├─sda1   8:1    0   128M  0 part /boot/efi
├─sda2   8:2    0     8G  0 part [SWAP]
└─sda3   8:3    0   290G  0 part /
sdb      8:16   1  29.3G  0 disk 
└─sdb1   8:17   1  29.3G  0 part 

lsblk is probably the quickest way to check for the device name. However, not all system ships with lsblk, so we might need to install it from the official package repository under the name util-linux.

7. fdisk

fdisk is a lightweight partition management utility available on most Linux distributions.

We can print the information regarding the currently attached devices through the –list option:

$ fdisk -l

After we execute the command, we can analyze the output:

...
Device        Start       End   Sectors  Size Type
/dev/sda1      2048    264191    262144  128M EFI System
/dev/sda2    264192  17041407  16777216    8G Linux swap
/dev/sda3  17041408 625141759 608100352  290G Linux filesystem

Disk /dev/sdb: 29.31 GiB, 31474155520 bytes, 61472960 sectors
Disk model: Mass Storage    
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: A440DCBF-1A4A-4234-9B90-C38DABF21039

Device     Start      End  Sectors  Size Type
/dev/sdb1   2048 61472767 61470720 29.3G Microsoft basic data

8. Conclusion

In this article, we looked at the different ways to check for the files related to USB storage devices under the /dev directory. We covered many different utilities such as dmesg, mount, and lsblk.


« 上一篇: Java Weekly, 第287期
» 下一篇: 使用Vim窗口