I have a question regarding the ports in Linux. If I connect my device via USB and want to check its port I can't do it using the command lsusb, which only specifies bus number and device number on this bus:
[ziga@Ziga-PC ~]$ lsusb
Bus 003 Device 007: ID 0403:6001 Future Technology Devices International, Ltd FT232 USB-Serial (UART) IC
Is there a command that tells me the port the device is connected to directly? Only way to do this until now was to disconect and reconnect and using the command:
[ziga@Ziga-PC ~]$ dmesg | grep tty
[ 0.000000] console [tty0] enabled
[ 0.929510] 00:09: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A
[ 4.378109] systemd[1]: Starting system-getty.slice.
[ 4.378543] systemd[1]: Created slice system-getty.slice.
[ 8.786474] usb 3-4.4: FTDI USB Serial Device converter now attached to ttyUSB0
In the last line it can be seen that my device is connected to /dev/ttyUSB0.
Best Answer
I'm not quite certain what you're asking. You mention 'port' several times, but then in your example, you say the answer is
/dev/ttyUSB0
, which is a device dev path, not a port. So this answer is about finding the dev path for each device.Below is a quick and dirty script which walks through devices in
/sys
looking for USB devices with aID_SERIAL
attribute. Typically only real USB devices will have this attribute, and so we can filter with it. If we don't, you'll see a lot of things in the list that aren't physical devices.On my system, this results in the following:
Explanation:
Devices which show up in
/dev
have adev
file in their/sys
directory. So we search for directories matching this criteria.We want the directory path, so we strip off
/dev
.This gives us the path in
/dev
that corresponds to this/sys
device.This filters out things which aren't actual devices. Otherwise you'll get things like USB controllers & hubs. The
exit
exits the subshell, which flows to the next iteration of the loop.The
udevadm info -q property --export
command lists all the device properties in a format that can be parsed by the shell into variables. So we simply calleval
on this. This is also the reason why we wrap the code in the parenthesis, so that we use a subshell, and the variables get wiped on each loop.More filtering of things that aren't actual devices.
I hope you know what this line does :-)