13

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?

3
  • 1
    Browse the directories under /dev/disk/, the by-label and by-id paths might be of use. – lornix Jun 13 '14 at 9:54
  • Thanks, but /dev/disk contains only storage devices. Say, there's nothing about /dev/ttyUSB0. – Dmitry Frank Jun 13 '14 at 17:58
  • What is your use case? What's special about /dev/ttyUSB0 etc? – Faheem Mitha Apr 1 '16 at 9:39
6

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
  • 5
    This is not the way one can get device node like /dev/ttyUSB0. – Dmitry Frank Nov 12 '14 at 8:20
5

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
  • This won't work if the USB device is not a block device. – Kyle Falconer Mar 29 '20 at 19:12
0

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

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.