I need to get the device names of all connected USB disks (ie sdd
).
I have 3 USB disks plugged in, and 2 SATA disks:
$ find /sys/devices/ -name block
/sys/devices/pci0000:00/0000:00:14.0/usb3/3-7/3-7:1.0/host5/target5:0:0/5:0:0:0/block
/sys/devices/pci0000:00/0000:00:14.0/usb4/4-2/4-2:1.0/host6/target6:0:0/6:0:0:0/block
/sys/devices/pci0000:00/0000:00:14.0/usb4/4-5/4-5:1.0/host4/target4:0:0/4:0:0:0/block
/sys/devices/pci0000:00/0000:00:1f.2/ata1/host0/target0:0:0/0:0:0:0/block
/sys/devices/pci0000:00/0000:00:1f.2/ata2/host1/target1:0:0/1:0:0:0/block
I want to ignore the SATA disks, but I need to list all the USB disks.
In the terminal, I can us ls
and it will give me sdd
:
$ ls /sys/devices/pci0000:00/0000:00:14.0/usb3/3-7/3-7:1.0/host5/target5:0:0/5:0:0:0/block
sdd
But I need to use this in a script. I need to iterate over all USB disks, and I don't know the exact path in advance, so I have to use wildcards (*
or ?
):
for DISK in $(ls /sys/devices/pci0000:00/0000:00:14.0/usb?/*/*:1.0/host?/target?:0:0/?:0:0:0/block) ; do
echo /dev/$DISK
done
the above only works if one USB disk is plugged in. If two or more disks are plugend in, I get sdd
as well as the /sys
path, which I don't want, ie:
/dev//sys/devices/pci0000:00/0000:00:14.0/usb3/3-7/3-7:1.0/host5/target5:0:0/5:0:0:0/block:
/dev/sdd
/dev//sys/devices/pci0000:00/0000:00:14.0/usb4/4-2/4-2:1.0/host6/target6:0:0/6:0:0:0/block:
/dev/sde
/dev//sys/devices/pci0000:00/0000:00:14.0/usb4/4-5/4-5:1.0/host4/target4:0:0/4:0:0:0/block:
/dev/sdc
how can I iterate only over sdd
sde
sdc
?
I am looking for a solution not using udev
infrastructure, ie /dev/disk/by-path/