How to enumerate USB block devices (USB memory sticks and HDDs)

How to enumerate USB block devices (USB memory sticks and HDDs)

There is also a method of How to enumerate USB block devices (USB memory and HDD) (Part 2).

Method

ls /sys/block/ -1 | \
  xargs -I{} echo /sys/block/{} | \
  xargs readlink | \
  grep usb | sed -e 's!.*/\([a-z]\+\)!\1!'

Commentary

Block device enumeration

ls /sys/block/ -l
Total 0
lrwxrwxrwx 1 root root 0 December 27 19:08 loop0 -> ../devices/virtual/block/loop0
lrwxrwxrwx 1 root root 0 December 27 19:08 loop1 -> ../devices/virtual/block/loop1
lrwxrwxrwx 1 root root 0 December 27 19:08 loop10 -> ../devices/virtual/block/loop10
lrwxrwxrwx 1 root root 0 December 27 19:08 loop2 -> ../devices/virtual/block/loop2
lrwxrwxrwx 1 root root 0 December 27 19:08 loop3 -> ../devices/virtual/block/loop3
lrwxrwxrwx 1 root root 0 December 27 19:08 loop4 -> ../devices/virtual/block/loop4
lrwxrwxrwx 1 root root 0 December 27 19:08 loop5 -> ../devices/virtual/block/loop5
lrwxrwxrwx 1 root root 0 December 27 19:08 loop6 -> ../devices/virtual/block/loop6
lrwxrwxrwx 1 root root 0 December 27 19:08 loop7 -> ../devices/virtual/block/loop7
lrwxrwxrwx 1 root root 0 December 27 19:08 loop8 -> ../devices/virtual/block/loop8
lrwxrwxrwx 1 root root 0 December 27 19:08 loop9 -> ../devices/virtual/block/loop9
lrwxrwxrwx 1 root root 0 December 27 19:08 sda -> ../devices/pci0000:00/0000:00:17.0/ata1/host0/target0:0:0/0:0:0:0/block/sda
lrwxrwxrwx 1 root root 0 December 27 19:19 sdb -> ../devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.0/host3/target3:0:0/3:0:0:0/block/sdb
lrwxrwxrwx 1 root root 0 December 27 19:08 sr0 -> ../devices/pci0000:00/0000:00:17.0/ata2/host1/target1:0:0/1:0:0:0/block/sr0
t

Enumerate USB block devices

$ ls /sys/block/ -1 | xargs -I{} echo /sys/block/{} | xargs readlink | grep usb
../devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.0/host3/target3:0:0/3:0:0:0/block/sdb

Extract the part of the USB block device

The last sdb part of ... block/sdb by using a regular expression like sed -e's!. */\ ([Az] \ + \)! \ 1!' Is extracted.

Finally, you can extract the part of the device file (such as sdb) with the following command.

ls /sys/block/ -1 | \
  xargs -I{} echo /sys/block/{} | \
  xargs readlink | \
  grep usb | sed -e 's!.*/\([a-z]\+\)!\1!'

You can output in the format / dev/sdb with the following command.

ls /sys/block/ -1 | \
  xargs -I{} echo /sys/block/{} | \
  xargs readlink | \
  grep usb | sed -e 's!.*/\([a-z]\+\)!\1!' | \
  xargs -I{} echo /dev/{}

Reference information

Block device size

You can get the sector size of the block device of / dev/sdb with the following command.

$ cat /sys/block/sdb/size 
15646720

You can get the size of the entire block device in bytes by multiplying the value of / sys/block/sdb/size by 512.

Recommended Posts

How to enumerate USB block devices (USB memory sticks and HDDs)
How to enumerate USB block devices (USB memory and HDD) (Part2)
How to use Python zip and enumerate
How to install and use Tesseract-OCR
How to install and configure blackbird
How to use .bash_profile and .bashrc
How to install CUDA and nvidia-driver
How to install and use Graphviz
How to solve slide puzzles and 15 puzzles
[Python] How to use the enumerate function (extract the index number and element)
[Linux] How to subdivide files and folders
How to package and distribute Python scripts
How to split and save a DataFrame
How to install and use pandas_datareader [Python]
How to make multi-boot USB (Windows 10 compatible)
[Python] How to calculate MAE and RMSE
How to use is and == in Python
How to use pandas Timestamp and date_range
How to install fabric and basic usage
How to write pydoc and multi-line comments