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.
-
1question should migrate to askubuntu – Vishwanath Dalvi May 06 '11 at 12:38
-
11@Viswanathan: "Linux" isn't Ubuntu (which is, of course, why having a separate askubuntu site at all is stupid, but I digress) – Wooble May 06 '11 at 12:41
-
Is this a programming question? Because the answers so far are not programming-related. – Gabe May 06 '11 at 13:04
-
3This is not an Ubuntu specific question. I was looking for a programming library solution, but I can use something like Python os module to list the devices with Ignacio Vazquez-Abrams solution anyway. – Marek Sapota May 06 '11 at 13:30
-
6`/dev/disk/by-id/usb-*` is all USB mass storage devices currently connected. – Ignacio Vazquez-Abrams May 06 '11 at 12:48
-
1I'm am also writing a Python script that needs to find a particular USB drive. This seems like a perfectly reasonable place to ask and receive help on this matter. – Nathan Hartley Nov 02 '11 at 01:40
-
Found a similar question here http://stackoverflow.com/questions/5109879/usb-devices-udev-and-d-bus – Nathan Hartley Nov 02 '11 at 02:09
-
@IgnacioVazquez-Abrams you should post your answer as a real answer – pistache Nov 21 '12 at 08:06
-
use lsusb. and asking on linux forums, you will get more techniques – alinsoar Nov 21 '12 at 10:20
-
@pistache: I did. A moderator deleted it. – Ignacio Vazquez-Abrams Nov 21 '12 at 23:51
5 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.

- 5,497
- 2
- 20
- 26
After all this time the question got unlocked again…
In the end I used UDisks via the D‐Bus interface like shown here.

- 1
- 1

- 20,103
- 3
- 34
- 47
Sometime back i got this small script ( it's not mine ) but it surely helped me alot putting just for reference
#!/usr/bin/python
import sys
import usb.core
# find USB devices
dev = usb.core.find(find_all=True)
# loop through devices, printing vendor and product ids in decimal and hex
for cfg in dev:
try:
#print dir(cfg)
sys.stdout.write('Decimal VendorID=' + str(cfg.idVendor) + ' & ProductID=' + str(cfg.bDeviceClass) + ' ' + str(cfg.product) + ' ' + str(cfg.bDeviceSubClass)+ ' ' + str(cfg.manufacturer)+'\n')
except:
print

- 2,683
- 2
- 29
- 44
This is what I use from bash: lsblk --pairs --nodeps | grep 'RM="1"'
Sample output: NAME="sda" MAJ:MIN="8:0" RM="1" SIZE="59.5G" RO="0" TYPE="disk" MOUNTPOINT=""
Note it is listing the devices, not its partitions. If you like to see the partitions also, lsblk --pairs | grep 'RM="1"'

- 139
- 1
- 8
Any reason not to just parse out the results from lsusb
? I'm sure there are modules for this, but then again, easy is sometimes best.
I can't help you with Python, in Perl I might do:
#!/usr/bin/env perl
use strict;
use warnings;
my @data;
foreach (`lsusb`) {
next unless /Bus (\S+) Device (\S+): ID (\S+) (.*)/;
push @data, { bus => $1, device => $2, id => $3, info => $4 };
}
use Data::Printer;
p @data;
which, on my computer, results in
[
[0] {
bus 005,
device 001,
id "1d6b:0001",
info "Linux Foundation 1.1 root hub"
},
[1] {
bus 004,
device 001,
id "1d6b:0001",
info "Linux Foundation 1.1 root hub"
},
[2] {
bus 003,
device 001,
id "1d6b:0001",
info "Linux Foundation 1.1 root hub"
},
[3] {
bus 002,
device 001,
id "1d6b:0001",
info "Linux Foundation 1.1 root hub"
},
[4] {
bus 001,
device 003,
id "0bda:0158",
info "Realtek Semiconductor Corp. USB 2.0 multicard reader"
},
[5] {
bus 001,
device 002,
id "064e:a129",
info "Suyin Corp. "
},
[6] {
bus 001,
device 001,
id "1d6b:0002",
info "Linux Foundation 2.0 root hub"
}
]
Note that Data::Printer
and its p
function are human-friendly object dumping for inspection purposes only.

- 20,180
- 5
- 49
- 104
-
I am not good with perl but... I think a better way (generally) is to use the udev or some usb module for perl instead of parsing `ls` commands. – spinus Dec 05 '12 at 20:13
-
Certainly both perl and python have modules for this purpose, still the output of `lsusb` is valid and gets the job done fast. As always, the level of rigor employed is up to the author and his/her task. – Joel Berger Dec 05 '12 at 23:22