22

I would like to list ONLY devices connected via usb.

The problem is that Ubuntu seems to see all thumb drives as removable (in /sys/block/*/removable), but it does not see external hard drives the same way.

This is a bit more specific than How to detect an USB device?

muru
  • 197,895
  • 55
  • 485
  • 740

6 Answers6

15

If you are looking for the mounted disks, a simple

df

will list them along with all your other disks along with some useful info.

Jazz
  • 2,737
10

I'd recommend checking the udev properties of the devices, specifically the ID_BUS property:

for device in /sys/block/*
do
    if udevadm info --query=property --path=$device | grep -q ^ID_BUS=usb
    then
        echo $device
    fi
done
Jeremy Kerr
  • 27,199
3
sudo lshw -class disk -short

Will list all devices and their human-readable names.

Emmit
  • 171
3

I think

lsusb

will give you what you want, at least if I understand what you're asking. (Of course, it lists all usb devices, not just storage.)

Kelley
  • 33,062
2

Listing only attached USB storage devices

Edit: When I have some time, I'll revisit this post to make it list only USB devices. For now, it lists all devices mounted in /media which may be good enough for some people.

Listing all devices mounted in /media

All of the other answers here appear to fail in filtering out either non USB storage devices or non-storage USB devices. Here is a command that should list only storage devices attached via USB. One exception, which is likely not to matter to anyone is that this will not display connected USB optical drives with mounted media.

Requirements for this to work

  • USB devices must be mounted. Ubuntu desktop OSs typically auto-mount by default
  • Media must be mounted in the /media directory. If your USB device is configured in fstab to mount somewhere else, you'd have to tweak the following commands

Listing USB storage devices
In my particular case for a script I'm writing, I list individual partitions. Here are two commands. One will list partitions of attached devices, and the other will simply list devices.

  • Listing partitions:
    lsblk | grep /media | grep -oP "sd[a-z][0-9]?" | awk '{print "/dev/"$1}'
    Sample Output:

    /dev/sdd1
    /dev/sdi1
    /dev/sdj1
    /dev/sdj2
    
  • Listing devices:
    lsblk | grep /media | grep -oP "sd[a-z]" | awk '{print "/dev/"$1}' | sort | uniq
    Sample Output:

    /dev/sdd
    /dev/sdi
    /dev/sdj
    
b_laoshi
  • 4,660
  • 4
  • 25
  • 46
  • What about partitions of internal disks that go mounted in /media? – muru Aug 21 '17 at 01:16
  • That would present a problem, but one that's not likely to affect most users. I'll give it some thought and update later. – b_laoshi Aug 21 '17 at 09:08
1

lsblk can show you the connection type, and makes it easy to configure the output you want.

Use lsblk -O to see it's full output, or check lsblk --help to see the available output columns. Then use -o (or --output) to list the columns you want.

For example:

lsblk -o NAME,TYPE,FSTYPE,RM,TRAN,SIZE,MOUNTPOINT

This uses

  • TRAN : the "device transport type". Usually "usb" or "sata".
  • RM : "removable device".
  • SIZE : to be able to exclude empty USB slots. These may show up but with a size of 0.

To use it in a script, there is the -P option : "use key='value' output format".

So for example:

lsblk -P -b -o 'KNAME,TYPE,TRAN,RM,FSTYPE,SIZE,MOUNTPOINT' \
| while read -r vars; do
    eval "$vars"
    if ((SIZE)) && [ "$TRAN" = "usb" ]; then
       echo "$KNAME is a USB disk: $vars"
    fi
done
mivk
  • 5,312