7

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/

8

You can do it with lsblk command.

lsblk -l -o name,tran gives

NAME TRAN
sda  sata
sda1 
sdb  usb
sdc  usb
sr0  sata

-l stands for "list" format, so it's easier to parse. Otherwise, you would get a tree format like this:

NAME   TRAN
sda    sata
└─sda1
sdb    usb
sr0    sata

Specifying other flags will give you more information like FSTYPE, LABEL, UUID, MOUNTPOINT and many other, just run lsblk --help to see all options.

You may want to use --paths --noheadings --scsi flags to have output printed like this:

sata   /dev/sda
usb    /dev/sdb
usb    /dev/sdc
sata   /dev/sr0

and then grep over the input to filter out those lines with usb at the beginning of the line.

| improve this answer | |
4

Do lsusb, it gives the name of the devices connected via the USB bus.

As in:

$ lsusb
Bus 002 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 004: ID 0421:0802 Nokia Mobile Phones CA-42 Phone Parent
Bus 001 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

See also man lsusb

Description

lsusb is a utility for displaying information about USB buses in the system and the devices connected to them.

| improve this answer | |
  • thanks, but how can i use that in my for loop example ? – Martin Vegter May 29 '17 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.