There is also a method of How to enumerate USB block devices (USB memory and HDD) (Part 2).
ls /sys/block/ -1 | \
xargs -I{} echo /sys/block/{} | \
xargs readlink | \
grep usb | sed -e 's!.*/\([a-z]\+\)!\1!'
/ sys/block /
.usb
.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
ls/sys/block/-1
to xargs
, and then use readlink
to enumerate the entities of the symbolic link.$ 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
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/{}
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