Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listing all USB drives in Linux

How can I get a list of removable drives (plugged into USB) in Linux? I'm fine with using KDE, GNOME or other DE libraries if it would make things easier.

like image 895
Marek Sapota Avatar asked May 06 '11 12:05

Marek Sapota


People also ask

How do I list all USB devices?

Enter the following command: Get-PnpDevice -PresentOnly | Where-Object { $_. InstanceId -match '^USB' } . That command will show a list of all present USB devices.

How do I list all USB devices in Ubuntu?

The number one way an Ubuntu user can view all connected USB devices is with the lsusb command. This command literally means “list USB,” and it does exactly that — it lists all of your USB devices, their IDs, names, etc. To get started, open up a terminal window on the Ubuntu desktop.


2 Answers

I think a nice idea is to use udev interface from python.

Small example (of course in your case you have adjust some filtering):

In [1]: import pyudev
In [2]: pyudev.Context()
In [3]: ctx = pyudev.Context()
In [4]: list(ctx.list_devices(subsystem='usb'))
Out[4]: 
[Device(u'/sys/devices/pci0000:00/0000:00:1a.0/usb2'),
 Device(u'/sys/devices/pci0000:00/0000:00:1a.0/usb2/2-0:1.0'),
 Device(u'/sys/devices/pci0000:00/0000:00:1a.0/usb2/2-2'),

It is a good way in most cases as new systems use udev.

like image 109
spinus Avatar answered Oct 06 '22 08:10

spinus


After all this time the question got unlocked again…

In the end I used UDisks via the D‐Bus interface like shown here.

like image 27
Marek Sapota Avatar answered Oct 06 '22 07:10

Marek Sapota