16

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?

14

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.

  • 2
    but this gives all block devices, not just those attached via USB. – Jeremy Kerr Aug 22 '17 at 2:36
7

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
  • This spits out devices for my built in card reader as well even when there are not cards inserted. – b_laoshi Aug 22 '17 at 2:15
  • @b_laoshi: yep, those will still report as storage devices, but with no media present. – Jeremy Kerr Aug 22 '17 at 2:32
2

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.)

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
    
  • What about partitions of internal disks that go mounted in /media? – muru Aug 21 '17 at 1: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 9:08

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.