Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to make a script that takes the product and vendor id printed by using lsusb, then checking against this ID to find the USB device's directory in /sys/bus/usb/devices.

I initially thought the Bus and Device number printed by lsusb would point to the appropriate folder. For example, if Bus = 002 and Device = 002, the USB's directory would be /usb/devices/2-2. Unfortunately, this turned out to not be the case.

I can manually find the appropriate folder using this command I found in another thread:

for X in /sys/bus/usb/devices/*; do 
    echo "$X"
    cat "$X/idVendor" 2>/dev/null 
    cat "$X/idProduct" 2>/dev/null

However, I need a script that can automate finding this folder.

share|improve this question
up vote 2 down vote accepted

If I understood your question, the following script should do the job:


if [ $# -ne 2 ];then
  echo "Usage: `basename $0` idVendor idProduct"
  exit 1

for X in /sys/bus/usb/devices/*; do 
    if [ "$1" == "$(cat "$X/idVendor" 2>/dev/null)" -a "$2" == "$(cat "$X/idProduct" 2>/dev/null)" ]
        echo "$X"
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.