I am making a script to determine if a connected USB device is a mass storage device, and if so, store the device number. I'd also like for it to work with multiple connected USB sticks.

Here is the basic idea:

If lsusb -t | grep Class=Mass matches, then save the device ID as a variable.

   |__ Port 4: Dev 5, If 0, Class=Mass Storage, Driver=usb-storage, 480M

In other words: How do I take "Dev x", if the words after it matches my search?

I am not that experienced with Linux, bash or regex. If this is weird or impractical, then I am open for other suggestions.

1 Answer 1


You could use awk:

lsusb -t | awk -F '[:,]''/Class=Mass/{print $2}'

We search for Class=Mass, the delimiter is : and , if we find Class=Mass then we print column two of the output, which will leave you with:

Dev 5

You must log in to answer this question.

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