How to get device filename from lsusb output or by device path
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 Answers 3
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).
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.
#!/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*\K5+'` # Read the device number: DEV=`echo $DATA | grep -Po 'Device 0*\K6+'` 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
$ ./usbname "$(lsusb | grep '')" /dev/sdb
I use this small bash function
getdevice() < idV=$idP=$ 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 >
# 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
How to find the Absolute device name
Using the path discovered in the previous question, look at this path in the /dev directory using a long listing ls command. Assuming this is in fact a soft link, what is the absolute device name which this link is pointing to?
- ls - list files in the file system.
- lsblk - list the block devices (i.e. drives)
- lspci - list the pci devices.
- lsusb - list the USB devices.
- lsdev - list all the devices.
4 Answers 4
"absolute device name" is not a term that is in common use as far as I know.
Maybe they mean an absolute path to a block device file that corresponds to the LVM logical volume.
Or given that they mention "soft link", which itself is more commonly referred to as "symbolic link" or "symlink", maybe they meant the absolute canonical (i.e. free or any symlink, . and .. components and with only one / in between each component and at the start and none at the end) path to a device file for that LV.
/dev/VG_name/LV_name is generally an absolute path to the LV_name LV in the VG_name volume group. /dev/mapper/VG_name-LV_name is often another one path to the same device file (though note that - in the VG/LV names are encoded as -- ).
Running readlink -e on both should give you the canonical absolute path of that file (generally something like /dev/dm- ).
Now, you do cp -a /dev/dm- /root/mydevice , and /root/mydevice will be another canonical absolute path to a block device file for that LV.
$ ls -l /root/mydevice brw-rw---- 1 root disk 253, 1 Sep 15 06:17 /dev/dm-1
What identifies the device here are the b (type: block), 253 (major number) and 1 (minor number) above that identify the device. You can recreate the same with the mknod command.
On Linux, to find the canonical name for that block device, you can look for 253:1 in /sys/class/block/*/dev :
$ grep -xFl 253:1 /sys/class/block/*/dev /sys/class/block/dm-1/dev
Or resolve the /sys/dev/block/253:1 symlink:
$ readlink -e /sys/dev/block/253:1 /sys/devices/virtual/block/dm-1
dm-1 being the canonical name here. udev would generally create a corresponding /dev/dm-1 file when that device is discovered.
Detecting the name of a network device in bash?
I'm trying to drum up a hack that will output the name of active network devices on one's computer via bash. I'm making it for work. How do I go about doing this? I don't want to just plainly use "wlan0" or "eth0" or any of that generic crap because some OSes use different names (Like pfSense for example).
3 Answers 3
It depends what you mean by 'active' - if you just want to see the names of all the network devices on the system, you can look at the contents of the /sys/class/net directory e.g.
$ ls /sys/class/net eth0 lo wlan0
To see the status, you could use the ip command on any link objects - you can parse the output to get the particular fields you want e.g. to see just the device name and state
$ ip -o link show | awk '' lo: UNKNOWN eth0: DOWN wlan0: UP
If you are running a modern desktop version of Ubuntu (with interfaces managed by the network-manager service), then you should be able to get a similar device status list using nmcli
$ nmcli dev status DEVICE TYPE STATE wlan0 802-11-wireless connected eth0 802-3-ethernet unavailable
or, to limit the output to particular fields in a way that's more easily used in a script
$ nmcli --terse --fields DEVICE,STATE dev status wlan0:connected eth0:unavailable
If you are using network-manager, you could also access device and connection properties via DBUS - see for example Dbus Tutorial - Fun with Network Manager
How to find the /dev name of my USB device
I am running a Ubuntu 11 guest on a Windows XP host with VMware. I want to format an SD card in Ubuntu, but I can't figure out which /dev/xxx device the SD card is. I plug the card into the built-in socket of my laptop. I "safely remove" the device in Windows. Then, I "connect" the PCMCIA reader in VMware. Now, I was supposed to see a new device like /dev/sdx , but it doesn't appear. How can I find what the name of my USB device's name and mount it? /var/log/message is empty. Here is the output of dmesg:
Oct 31 18:51:21 ubuntu kernel: [ 5268.927308] usb 2-1: new full speed USB device number 12 using uhci_hcd Oct 31 18:51:21 ubuntu mtp-probe: checking bus 2, device 12: "/sys/devices/pci0000:00/0000:00:11.0/0000:02:00.0/usb2/2-1" Oct 31 18:51:21 ubuntu mtp-probe: bus: 2, device: 12 was not an MTP device
13 Answers 13
Try lsblk . This is the output from my current setup:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 465.8G 0 disk ├─sda1 8:1 0 195.3G 0 part ├─sda2 8:2 0 4G 0 part [SWAP] └─sda3 8:3 0 266.5G 0 part / sdb 8:16 0 465.8G 0 disk └─sdb1 8:17 0 460.8G 0 part /home sr0 11:0 1 1024M 0 rom
ls -1 /dev > ~/after.txt diff ~/before.txt ~/after.txt
rishi@rishi-Inspiron-1018:~$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 232.9G 0 disk ├─sda1 8:1 0 230.9G 0 part / ├─sda2 8:2 0 1K 0 part └─sda5 8:5 0 2G 0 part [SWAP] sdb 8:16 1 7.4G 0 disk └─sdb1 8:17 1 7.4G 0 part /media/3765-3233
So now I know my SD card is in /dev/sdb1 .
It'll appear in /dev only if udev is present (or an equivalent), and as a rule to create the node in it. Does not mean it can't be used, you can create the node where you want, since you have the Major/Minor number in lsblk.
Using lsblk with some custom output columns I was able to find a more precise solution. See:
$ lsblk -o NAME,MAJ:MIN,RM,SIZE,RO,TYPE,MOUNTPOINT,VENDOR,HOTPLUG NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT VENDOR HOTPLUG sda 8:0 0 223.6G 0 disk ATA 0 ├─sda1 8:1 0 512M 0 part /boot/efi 0 └─sda2 8:2 0 223.1G 0 part / 0 sdb 8:16 1 238.3G 0 disk SanDisk 1 └─sdb1 8:17 1 238.3G 0 part /media/my-host/AAAA-BBBB 1 sdc 8:32 1 59.5G 0 disk SanDisk 1 └─sdc1 8:33 1 59.5G 0 part /media/my-host/AAAA-BBBB 1
Using lsblk -h it's possible to see all available output columns, and I added the VENDOR and HOTPLUG columns.
Looking at my output it's easy to see that I have 2 HOTPLUG devices (usb devices in my case) from SanDisk
Try using lsusb to see if the sd card reader is picked up in virtualbox. Another option would be to use palimpsest (also known by its menu entry, Disk Utility.)
Don't be afraid to use the GUI programs - they do work! If palimpsest sees your device, it will tell you the device node, so you can use the CLI utilities.
If it has a label use /dev/disk/by-label/[USB label] to format the disk
USBs are usually /dev/sb followed by a number, or something along thoses lines if I'm not completely correct, it might be /dev/sdb followed by a number but not normally /dev/sda, so if there's only one plugged in that should do it
sudo fdisk -l will list the partition table on all available hard disks; it uses the Linux naming scheme for disks, handy for troubleshooting and remembering which partition is where.
Use man fdisk for more information on the parameters of fdisk .
Open a terminal and run ls /dev before you enable that USB device in vmware. Run ls /dev/ again after you enabled it and look for what pops up
this is the most painful way of doing it, not to mention, many devices have numerous instances in /dev/ once they get initialized.
You could use bootiso utility, which has a flag to do exactly that:
> bootiso -l Listing USB drives available in your system: NAME HOTPLUG SIZE STATE TYPE sdd 1 14,7G running disk sde 1 961M running disk
curl -L https://rawgit.com/jsamr/bootiso/latest/bootiso -O chmod +x bootiso
# dir /dev/disk/by-path/ total 0 drwxr-xr-x. 2 root root 180 Aug 26 15:51 . drwxr-xr-x. 6 root root 120 Aug 25 17:20 .. lrwxrwxrwx. 1 root root 9 Aug 25 17:12 pci-0000:00:03.2-usb-0:3:1.0-scsi-0:0:0:0 -> ../../sr0 lrwxrwxrwx. 1 root root 9 Aug 26 15:51 pci-0000:00:03.2-usb-0:4.1.1:1.0-scsi-0:0:0:0 -> ../../sdb lrwxrwxrwx. 1 root root 10 Aug 26 15:51 pci-0000:00:03.2-usb-0:4.1.1:1.0-scsi-0:0:0:0-part1 -> ../../sdb1 lrwxrwxrwx. 1 root root 10 Aug 26 15:51 pci-0000:00:03.2-usb-0:4.1.1:1.0-scsi-0:0:0:0-part2 -> ../../sdb2 lrwxrwxrwx. 1 root root 10 Aug 26 15:51 pci-0000:00:03.2-usb-0:4.1.1:1.0-scsi-0:0:0:0-part3 -> ../../sdb3 lrwxrwxrwx. 1 root root 9 Aug 25 17:12 pci-0000:07:00.0-scsi-0:2:0:0 -> ../../sda lrwxrwxrwx. 1 root root 10 Aug 25 17:12 pci-0000:07:00.0-scsi-0:2:0:0-part1 -> ../../sda1
# dir /dev/disk/by-id/ total 0 drwxr-xr-x. 2 root root 280 Aug 27 01:20 . drwxr-xr-x. 6 root root 120 Aug 25 17:20 .. lrwxrwxrwx. 1 root root 10 Aug 25 17:20 dm-name-VG_RAID_201010081812-LV_RAID_201010081617 -> ../../dm-0 lrwxrwxrwx. 1 root root 10 Aug 27 01:20 dm-name-top4 -> ../../dm-2 lrwxrwxrwx. 1 root root 10 Aug 25 17:20 dm-uuid-LVM-XCQXKaPB3snmEgx9ZNGRssIxGmJPje6TXKMLyop5meKH3x3KiJ1H1q3aoqgXpci1 -> ../../dm-0 lrwxrwxrwx. 1 root root 9 Aug 25 17:12 scsi-36842b2b04963f900144207e1091bf90c -> ../../sda lrwxrwxrwx. 1 root root 10 Aug 25 17:12 scsi-36842b2b04963f900144207e1091bf90c-part1 -> ../../sda1 lrwxrwxrwx. 1 root root 9 Aug 25 17:12 usb-ASUS_SBW-06D2X-U_3248433031354B4A30303030-0:0 -> ../../sr0 lrwxrwxrwx. 1 root root 9 Aug 26 15:51 usb-ST916041_2AS_579FFFFFFFFF-0:0 -> ../../sdb lrwxrwxrwx. 1 root root 10 Aug 26 15:51 usb-ST916041_2AS_579FFFFFFFFF-0:0-part1 -> ../../sdb1 lrwxrwxrwx. 1 root root 10 Aug 26 15:51 usb-ST916041_2AS_579FFFFFFFFF-0:0-part2 -> ../../sdb2 lrwxrwxrwx. 1 root root 10 Aug 26 15:51 usb-ST916041_2AS_579FFFFFFFFF-0:0-part3 -> ../../sdb3 lrwxrwxrwx. 1 root root 9 Aug 25 17:12 wwn-0x6842b2b04963f900144207e1091bf90c -> ../../sda lrwxrwxrwx. 1 root root 10 Aug 25 17:12 wwn-0x6842b2b04963f900144207e1091bf90c-part1 -> ../../sda1