How to get device filename from lsusb output or by device path
Solution 1
Supposing I'm trying to find the device for my UVC camera, lsusb gives me:
Bus 001 Device 004: ID 1e4e:0102 Cubeternet GL-UPC822 UVC WebCam
The device filename is then /dev/bus/usb/001/004
(first component is bus id, next is device id).
Solution 2
I've just built a script for that, it's not pretty but works for me.
I tested this script on Arch Linux with this configurations:
$ uname -a
Linux 4.4.13-1-lts #1 SMP Wed Jun 8 16:44:31 CEST 2016 x86_64 GNU/Linux
And my device name is /dev/sdb
which is quite different from yours, I hope
it will work for you as well.
Also note that this script depends on usbutils
package for usb-devices
program, I believe its installed by default on all linux, but i might be wrong.
Script usbname
:
#!/usr/bin/bash
# Input should be a single line from lsusb output:
DATA=$1
# Read the bus number:
BUS=`echo $DATA | grep -Po 'Bus 0*\K[1-9]+'`
# Read the device number:
DEV=`echo $DATA | grep -Po 'Device 0*\K[1-9]+'`
FOUND=false
USB_Serial=""
# Search for the serial number of the PenDrive:
while read line
do
if [ $FOUND == true ]; then
USB_Serial=`echo "$line" | grep -Po 'SerialNumber=\K.*'`
if [ "$USB_Serial" != "" ]; then
break;
fi
fi
if [ "`echo "$line" | grep -e "Bus=0*$BUS.*Dev#= *$DEV"`" != "" ]; then
FOUND=true
fi
done <<< "$(usb-devices)"
# Get the base name of the block device, e.g.: "sdx"
BASENAME=`file /dev/disk/by-id/* | grep -v 'part' | grep -Po "$USB_Serial.*/\K[^/]+$"`
# Build the full address, e.g.: "/dev/sdx"
NAME="/dev/$BASENAME"
# Output the address:
echo $NAME
Usage:
$ ./usbname "$(lsusb | grep '<my_usb_label_or_id>')"
/dev/sdb
Solution 3
I use this small bash function
getdevice() {
idV=${1%:*}
idP=${1#*:}
for path in `find /sys/ -name idVendor | rev | cut -d/ -f 2- | rev`; do
if grep -q $idV $path/idVendor; then
if grep -q $idP $path/idProduct; then
find $path -name 'device' | rev | cut -d / -f 2 | rev
fi
fi
done
}
Example
# lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 005: ID 8087:0a2b Intel Corp.
Bus 001 Device 012: ID 0bda:2832 Realtek Semiconductor Corp. RTL2832U DVB-T
Bus 001 Device 053: ID 051d:0002 American Power Conversion Uninterruptible Power Supply
Bus 001 Device 051: ID 1cf1:0030 Dresden Elektronik
Bus 001 Device 006: ID 1a86:7523 QinHeng Electronics HL-340 USB-Serial adapter
Bus 001 Device 004: ID 05e3:0606 Genesys Logic, Inc. USB 2.0 Hub / D-Link DUB-H4 USB 2.0 Hub
Bus 001 Device 003: ID 0658:0200 Sigma Designs, Inc. Aeotec Z-Stick Gen5 (ZW090) - UZB
Bus 001 Device 002: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
and corresponding devices
# getdevice 051d:0002
hiddev0
hidraw0
# getdevice 1a86:7523
ttyUSB0
# getdevice 0658:0200
ttyACM1
Related videos on Youtube
Dmitry Frank
I'm a passionate software engineer with strong background in low-level parts (MCU real-time kernels, C, Assembler), and experienced in higher-level technologies as well: Go, C++, JavaScript, and many others. Author of the well-formed and carefully tested real-time kernel for 16- and 32-bit MCUs: TNeo, which is now used by several companies. One of my hobby projects is a geeky bookmarking service written in Go and PostgreSQL: Geekmarks. Some of my articles: How I ended up writing a new real-time kernel How do JavaScript closures work under the hood Unit-testing (embedded) C applications with Ceedling Object-oriented techniques in C See more at dmitryfrank.com
Updated on September 18, 2022Comments
-
Dmitry Frank over 1 year
Related question: USB connect/disconnect notification
When the device is plugged/unplugged, I get instant notification, that's great. But to make it (almost) perfect, I also want to get device filename like
/dev/ttyUSB0
, and, even better, all symlinks to it.But, I can't find how to get this information from
udev
, or fromlsusb
, or somehow else. The only ID of the device I have is a device path like/devices/pci0000:00/0000:00:1d.0/usb5/5-1
. How to get device filename from it?-
lornix almost 10 yearsBrowse the directories under
/dev/disk/
, the by-label and by-id paths might be of use. -
Dmitry Frank almost 10 yearsThanks, but
/dev/disk
contains only storage devices. Say, there's nothing about/dev/ttyUSB0
. -
Alen Milakovic about 8 yearsWhat is your use case? What's special about
/dev/ttyUSB0
etc?
-
-
Charlie Chen about 4 yearsThis won't work if the USB device is not a block device.