9

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?

  • 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
4

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

  • 3
    This is not the way one can get device node like /dev/ttyUSB0. – Dmitry Frank Nov 12 '14 at 8:20
3

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

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.