18

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 from lsusb, 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?

4
  • 1
    Browse the directories under /dev/disk/, the by-label and by-id paths might be of use.
    – lornix
    Jun 13, 2014 at 9:54
  • 2
    Thanks, but /dev/disk contains only storage devices. Say, there's nothing about /dev/ttyUSB0. Jun 13, 2014 at 17:58
  • 1
    What is your use case? What's special about /dev/ttyUSB0 etc? Apr 1, 2016 at 9:39
  • /dev/ttyUSB0 would likely be a serial device.
    – My1
    Jul 18, 2023 at 8:13

3 Answers 3

15

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

1
  • 9
    This is not the way one can get device node like /dev/ttyUSB0. Nov 12, 2014 at 8:20
6

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
1
  • 3
    This won't work if the USB device is not a block device. Mar 29, 2020 at 19:12
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
1
  • Your script fails for me: find: File system loop detected; '/sys/kernel/debug/device_component' is part of the same file system loop as '/sys/kernel/debug'.
    – Blaine
    May 28, 2023 at 21:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .