command to determine ports of a device (like /dev/ttyUSB0) The Next CEO of Stack OverflowHow to match a ttyUSBX device to a usb serial deviceRunning python script, error appearsWD MyBook 3TB - Gparted failed & HDD partitions recovery?How do I access a USB serial port without an entry in /dev?Attaching USB-Serial device with custom PID to ttyUSB0 on embeddedSerial Port I/O Error on Intel NUC with Debian WheezyHow to associate physical usb port with usb device numberHow to get the specific serial port on usb-serial hub in Linuxserial device output looks fine in gnu screen but garbled using catUSB ports keep resetting on LinuxPL2303/PL2303X USB-Serial driverGetting rid of a serial console on Respeaker Core V2
How do I solve this limit?
If the heap is initialized for security, then why is the stack uninitialized?
Which organization defines CJK Unified Ideographs?
How to safely derail a train during transit?
Why here is plural "We went to the movies last night."
What happens if you roll doubles 3 times then land on "Go to jail?"
Rotate a column
Increase performance creating Mandelbrot set in python
Why does standard notation not preserve intervals (visually)
What makes a siege story/plot interesting?
Term for the "extreme-extension" version of a straw man fallacy?
How to be diplomatic in refusing to write code that breaches the privacy of our users
Describing a person. What needs to be mentioned?
Implement the Thanos sorting algorithm
Horror movie/show or scene where a horse creature opens its mouth really wide and devours a man in a stables
Go Pregnant or Go Home
How to Reset Passwords on Multiple Websites Easily?
Only print output after finding pattern
Is a stroke of luck acceptable after a series of unfavorable events?
Failed to fetch jessie backports repository
Can a single photon have an energy density?
Anatomically Correct Mesopelagic Aves
How to make a variable always equal to the result of some calculations?
Why did we only see the N-1 starfighters in one film?
command to determine ports of a device (like /dev/ttyUSB0)
The Next CEO of Stack OverflowHow to match a ttyUSBX device to a usb serial deviceRunning python script, error appearsWD MyBook 3TB - Gparted failed & HDD partitions recovery?How do I access a USB serial port without an entry in /dev?Attaching USB-Serial device with custom PID to ttyUSB0 on embeddedSerial Port I/O Error on Intel NUC with Debian WheezyHow to associate physical usb port with usb device numberHow to get the specific serial port on usb-serial hub in Linuxserial device output looks fine in gnu screen but garbled using catUSB ports keep resetting on LinuxPL2303/PL2303X USB-Serial driverGetting rid of a serial console on Respeaker Core V2
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.
usb serial-port dmesg
add a comment |
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.
usb serial-port dmesg
What are you trying to accomplish? Do you want to associate a device in/dev
with an entry inlsusb
? Or do you want to list all devices in/dev
that are derived from a physical USB device? Or are you just wantingls /dev/ttyUSB*
?
– Patrick
Jul 11 '14 at 17:00
I just need a command which will give me port of a device and will not push me to disconnect and reconnect my devices.ls /dev/ttyUSB*
will only list maybee 10 ports but from this list I cannot tell which one is for my device.
– 71GA
Jul 15 '14 at 16:41
I found this to be helpful. lsusb -v
– tjaart55
yesterday
add a comment |
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.
usb serial-port dmesg
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.
usb serial-port dmesg
usb serial-port dmesg
edited Sep 18 '16 at 1:32
Jeff Schaller♦
44.1k1161142
44.1k1161142
asked Jul 11 '14 at 16:40
71GA71GA
47611126
47611126
What are you trying to accomplish? Do you want to associate a device in/dev
with an entry inlsusb
? Or do you want to list all devices in/dev
that are derived from a physical USB device? Or are you just wantingls /dev/ttyUSB*
?
– Patrick
Jul 11 '14 at 17:00
I just need a command which will give me port of a device and will not push me to disconnect and reconnect my devices.ls /dev/ttyUSB*
will only list maybee 10 ports but from this list I cannot tell which one is for my device.
– 71GA
Jul 15 '14 at 16:41
I found this to be helpful. lsusb -v
– tjaart55
yesterday
add a comment |
What are you trying to accomplish? Do you want to associate a device in/dev
with an entry inlsusb
? Or do you want to list all devices in/dev
that are derived from a physical USB device? Or are you just wantingls /dev/ttyUSB*
?
– Patrick
Jul 11 '14 at 17:00
I just need a command which will give me port of a device and will not push me to disconnect and reconnect my devices.ls /dev/ttyUSB*
will only list maybee 10 ports but from this list I cannot tell which one is for my device.
– 71GA
Jul 15 '14 at 16:41
I found this to be helpful. lsusb -v
– tjaart55
yesterday
What are you trying to accomplish? Do you want to associate a device in
/dev
with an entry in lsusb
? Or do you want to list all devices in /dev
that are derived from a physical USB device? Or are you just wanting ls /dev/ttyUSB*
?– Patrick
Jul 11 '14 at 17:00
What are you trying to accomplish? Do you want to associate a device in
/dev
with an entry in lsusb
? Or do you want to list all devices in /dev
that are derived from a physical USB device? Or are you just wanting ls /dev/ttyUSB*
?– Patrick
Jul 11 '14 at 17:00
I just need a command which will give me port of a device and will not push me to disconnect and reconnect my devices.
ls /dev/ttyUSB*
will only list maybee 10 ports but from this list I cannot tell which one is for my device.– 71GA
Jul 15 '14 at 16:41
I just need a command which will give me port of a device and will not push me to disconnect and reconnect my devices.
ls /dev/ttyUSB*
will only list maybee 10 ports but from this list I cannot tell which one is for my device.– 71GA
Jul 15 '14 at 16:41
I found this to be helpful. lsusb -v
– tjaart55
yesterday
I found this to be helpful. lsusb -v
– tjaart55
yesterday
add a comment |
5 Answers
5
active
oldest
votes
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 a ID_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.
#!/bin/bash
for sysdevpath in $(find /sys/bus/usb/devices/usb*/ -name dev); do
(
syspath="$sysdevpath%/dev"
devname="$(udevadm info -q name -p $syspath)"
[[ "$devname" == "bus/"* ]] && continue
eval "$(udevadm info -q property --export -p $syspath)"
[[ -z "$ID_SERIAL" ]] && continue
echo "/dev/$devname - $ID_SERIAL"
)
done
On my system, this results in the following:
/dev/ttyACM0 - LG_Electronics_Inc._LGE_Android_Phone_VS930_4G-991c470
/dev/sdb - Lexar_USB_Flash_Drive_AA26MYU15PJ5QFCL-0:0
/dev/sdb1 - Lexar_USB_Flash_Drive_AA26MYU15PJ5QFCL-0:0
/dev/input/event5 - Logitech_USB_Receiver
/dev/input/mouse1 - Logitech_USB_Receiver
/dev/input/event2 - Razer_Razer_Diamondback_3G
/dev/input/mouse0 - Razer_Razer_Diamondback_3G
/dev/input/event3 - Logitech_HID_compliant_keyboard
/dev/input/event4 - Logitech_HID_compliant_keyboard
Explanation:
find /sys/bus/usb/devices/usb*/ -name dev
Devices which show up in /dev
have a dev
file in their /sys
directory. So we search for directories matching this criteria.
syspath="$sysdevpath%/dev"
We want the directory path, so we strip off /dev
.
devname="$(udevadm info -q name -p $syspath)"
This gives us the path in /dev
that corresponds to this /sys
device.
[[ "$devname" == "bus/"* ]] && continue
This filters out things which aren't actual devices. Otherwise you'll get things like USB controllers & hubs.
eval "$(udevadm info -q property --export -p $syspath)"
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 call eval
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.
[[ -z "$ID_SERIAL" ]] && continue
More filtering of things that aren't actual devices.
echo "/dev/$devname - $ID_SERIAL"
I hope you know what this line does :-)
2
Thank you. I will learn a lot from your anwser and now I see that terms werent completely clear to me. Is there any shorter way? Maybee a command already integrated in the Linux itself?
– 71GA
Jul 17 '14 at 8:43
Nope. If there was a pre-existing command, I would happily recommend it.
– Patrick
Jul 17 '14 at 17:07
Suggest to change /bin/bash to /bin/sh and remove the ( ) for max compatibility
– albfan
Feb 5 at 14:46
add a comment |
You can use this command to explore your device if connected to usb0
:
udevadm info -a -p $(udevadm info -q path -n /dev/ttyUSB0)
The top answer didn't work on my machine, but this worked great. Thanks!
– johnny_boy
Jun 19 '17 at 14:45
add a comment |
You could try something like this below.
echo -n "/dev/"; dmesg | grep tty|grep USB|rev|awk 'print $1'|rev
ow my eyes!dmesg | awk '/tty/ && /USB/ print "/dev/"$1'
(The equivalent but clean...but still strange code; why not just ttyUSB together? and what was 2x rev for?)
– Peter
Nov 13 '17 at 11:40
add a comment |
Perhaps you would like to know just the path to USB-serial adapter that connected last?
dmesg | awk '/tty/ && /USB/ print "/dev/"$10'|tail -1
add a comment |
Assuming that you know what the device you plugged in is, in 14.04 Ubuntu, at least, there is the command usb-devices
that you can look through and find the information:
$ usb-devices
T: Bus=01 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=480 MxCh= 3
D: Ver= 2.00 Cls=09(hub ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
P: Vendor=1d6b ProdID=0002 Rev=04.04
S: Manufacturer=Linux 4.4.0-131-generic ehci_hcd
S: Product=EHCI Host Controller
S: SerialNumber=0000:00:1a.0
C: #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr=0mA
I: If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub
And the first line lists bus and port, as well as the device number that lsusb
gives.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f144029%2fcommand-to-determine-ports-of-a-device-like-dev-ttyusb0%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 a ID_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.
#!/bin/bash
for sysdevpath in $(find /sys/bus/usb/devices/usb*/ -name dev); do
(
syspath="$sysdevpath%/dev"
devname="$(udevadm info -q name -p $syspath)"
[[ "$devname" == "bus/"* ]] && continue
eval "$(udevadm info -q property --export -p $syspath)"
[[ -z "$ID_SERIAL" ]] && continue
echo "/dev/$devname - $ID_SERIAL"
)
done
On my system, this results in the following:
/dev/ttyACM0 - LG_Electronics_Inc._LGE_Android_Phone_VS930_4G-991c470
/dev/sdb - Lexar_USB_Flash_Drive_AA26MYU15PJ5QFCL-0:0
/dev/sdb1 - Lexar_USB_Flash_Drive_AA26MYU15PJ5QFCL-0:0
/dev/input/event5 - Logitech_USB_Receiver
/dev/input/mouse1 - Logitech_USB_Receiver
/dev/input/event2 - Razer_Razer_Diamondback_3G
/dev/input/mouse0 - Razer_Razer_Diamondback_3G
/dev/input/event3 - Logitech_HID_compliant_keyboard
/dev/input/event4 - Logitech_HID_compliant_keyboard
Explanation:
find /sys/bus/usb/devices/usb*/ -name dev
Devices which show up in /dev
have a dev
file in their /sys
directory. So we search for directories matching this criteria.
syspath="$sysdevpath%/dev"
We want the directory path, so we strip off /dev
.
devname="$(udevadm info -q name -p $syspath)"
This gives us the path in /dev
that corresponds to this /sys
device.
[[ "$devname" == "bus/"* ]] && continue
This filters out things which aren't actual devices. Otherwise you'll get things like USB controllers & hubs.
eval "$(udevadm info -q property --export -p $syspath)"
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 call eval
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.
[[ -z "$ID_SERIAL" ]] && continue
More filtering of things that aren't actual devices.
echo "/dev/$devname - $ID_SERIAL"
I hope you know what this line does :-)
2
Thank you. I will learn a lot from your anwser and now I see that terms werent completely clear to me. Is there any shorter way? Maybee a command already integrated in the Linux itself?
– 71GA
Jul 17 '14 at 8:43
Nope. If there was a pre-existing command, I would happily recommend it.
– Patrick
Jul 17 '14 at 17:07
Suggest to change /bin/bash to /bin/sh and remove the ( ) for max compatibility
– albfan
Feb 5 at 14:46
add a comment |
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 a ID_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.
#!/bin/bash
for sysdevpath in $(find /sys/bus/usb/devices/usb*/ -name dev); do
(
syspath="$sysdevpath%/dev"
devname="$(udevadm info -q name -p $syspath)"
[[ "$devname" == "bus/"* ]] && continue
eval "$(udevadm info -q property --export -p $syspath)"
[[ -z "$ID_SERIAL" ]] && continue
echo "/dev/$devname - $ID_SERIAL"
)
done
On my system, this results in the following:
/dev/ttyACM0 - LG_Electronics_Inc._LGE_Android_Phone_VS930_4G-991c470
/dev/sdb - Lexar_USB_Flash_Drive_AA26MYU15PJ5QFCL-0:0
/dev/sdb1 - Lexar_USB_Flash_Drive_AA26MYU15PJ5QFCL-0:0
/dev/input/event5 - Logitech_USB_Receiver
/dev/input/mouse1 - Logitech_USB_Receiver
/dev/input/event2 - Razer_Razer_Diamondback_3G
/dev/input/mouse0 - Razer_Razer_Diamondback_3G
/dev/input/event3 - Logitech_HID_compliant_keyboard
/dev/input/event4 - Logitech_HID_compliant_keyboard
Explanation:
find /sys/bus/usb/devices/usb*/ -name dev
Devices which show up in /dev
have a dev
file in their /sys
directory. So we search for directories matching this criteria.
syspath="$sysdevpath%/dev"
We want the directory path, so we strip off /dev
.
devname="$(udevadm info -q name -p $syspath)"
This gives us the path in /dev
that corresponds to this /sys
device.
[[ "$devname" == "bus/"* ]] && continue
This filters out things which aren't actual devices. Otherwise you'll get things like USB controllers & hubs.
eval "$(udevadm info -q property --export -p $syspath)"
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 call eval
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.
[[ -z "$ID_SERIAL" ]] && continue
More filtering of things that aren't actual devices.
echo "/dev/$devname - $ID_SERIAL"
I hope you know what this line does :-)
2
Thank you. I will learn a lot from your anwser and now I see that terms werent completely clear to me. Is there any shorter way? Maybee a command already integrated in the Linux itself?
– 71GA
Jul 17 '14 at 8:43
Nope. If there was a pre-existing command, I would happily recommend it.
– Patrick
Jul 17 '14 at 17:07
Suggest to change /bin/bash to /bin/sh and remove the ( ) for max compatibility
– albfan
Feb 5 at 14:46
add a comment |
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 a ID_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.
#!/bin/bash
for sysdevpath in $(find /sys/bus/usb/devices/usb*/ -name dev); do
(
syspath="$sysdevpath%/dev"
devname="$(udevadm info -q name -p $syspath)"
[[ "$devname" == "bus/"* ]] && continue
eval "$(udevadm info -q property --export -p $syspath)"
[[ -z "$ID_SERIAL" ]] && continue
echo "/dev/$devname - $ID_SERIAL"
)
done
On my system, this results in the following:
/dev/ttyACM0 - LG_Electronics_Inc._LGE_Android_Phone_VS930_4G-991c470
/dev/sdb - Lexar_USB_Flash_Drive_AA26MYU15PJ5QFCL-0:0
/dev/sdb1 - Lexar_USB_Flash_Drive_AA26MYU15PJ5QFCL-0:0
/dev/input/event5 - Logitech_USB_Receiver
/dev/input/mouse1 - Logitech_USB_Receiver
/dev/input/event2 - Razer_Razer_Diamondback_3G
/dev/input/mouse0 - Razer_Razer_Diamondback_3G
/dev/input/event3 - Logitech_HID_compliant_keyboard
/dev/input/event4 - Logitech_HID_compliant_keyboard
Explanation:
find /sys/bus/usb/devices/usb*/ -name dev
Devices which show up in /dev
have a dev
file in their /sys
directory. So we search for directories matching this criteria.
syspath="$sysdevpath%/dev"
We want the directory path, so we strip off /dev
.
devname="$(udevadm info -q name -p $syspath)"
This gives us the path in /dev
that corresponds to this /sys
device.
[[ "$devname" == "bus/"* ]] && continue
This filters out things which aren't actual devices. Otherwise you'll get things like USB controllers & hubs.
eval "$(udevadm info -q property --export -p $syspath)"
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 call eval
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.
[[ -z "$ID_SERIAL" ]] && continue
More filtering of things that aren't actual devices.
echo "/dev/$devname - $ID_SERIAL"
I hope you know what this line does :-)
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 a ID_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.
#!/bin/bash
for sysdevpath in $(find /sys/bus/usb/devices/usb*/ -name dev); do
(
syspath="$sysdevpath%/dev"
devname="$(udevadm info -q name -p $syspath)"
[[ "$devname" == "bus/"* ]] && continue
eval "$(udevadm info -q property --export -p $syspath)"
[[ -z "$ID_SERIAL" ]] && continue
echo "/dev/$devname - $ID_SERIAL"
)
done
On my system, this results in the following:
/dev/ttyACM0 - LG_Electronics_Inc._LGE_Android_Phone_VS930_4G-991c470
/dev/sdb - Lexar_USB_Flash_Drive_AA26MYU15PJ5QFCL-0:0
/dev/sdb1 - Lexar_USB_Flash_Drive_AA26MYU15PJ5QFCL-0:0
/dev/input/event5 - Logitech_USB_Receiver
/dev/input/mouse1 - Logitech_USB_Receiver
/dev/input/event2 - Razer_Razer_Diamondback_3G
/dev/input/mouse0 - Razer_Razer_Diamondback_3G
/dev/input/event3 - Logitech_HID_compliant_keyboard
/dev/input/event4 - Logitech_HID_compliant_keyboard
Explanation:
find /sys/bus/usb/devices/usb*/ -name dev
Devices which show up in /dev
have a dev
file in their /sys
directory. So we search for directories matching this criteria.
syspath="$sysdevpath%/dev"
We want the directory path, so we strip off /dev
.
devname="$(udevadm info -q name -p $syspath)"
This gives us the path in /dev
that corresponds to this /sys
device.
[[ "$devname" == "bus/"* ]] && continue
This filters out things which aren't actual devices. Otherwise you'll get things like USB controllers & hubs.
eval "$(udevadm info -q property --export -p $syspath)"
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 call eval
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.
[[ -z "$ID_SERIAL" ]] && continue
More filtering of things that aren't actual devices.
echo "/dev/$devname - $ID_SERIAL"
I hope you know what this line does :-)
answered Jul 16 '14 at 3:00
PatrickPatrick
51.3k11132183
51.3k11132183
2
Thank you. I will learn a lot from your anwser and now I see that terms werent completely clear to me. Is there any shorter way? Maybee a command already integrated in the Linux itself?
– 71GA
Jul 17 '14 at 8:43
Nope. If there was a pre-existing command, I would happily recommend it.
– Patrick
Jul 17 '14 at 17:07
Suggest to change /bin/bash to /bin/sh and remove the ( ) for max compatibility
– albfan
Feb 5 at 14:46
add a comment |
2
Thank you. I will learn a lot from your anwser and now I see that terms werent completely clear to me. Is there any shorter way? Maybee a command already integrated in the Linux itself?
– 71GA
Jul 17 '14 at 8:43
Nope. If there was a pre-existing command, I would happily recommend it.
– Patrick
Jul 17 '14 at 17:07
Suggest to change /bin/bash to /bin/sh and remove the ( ) for max compatibility
– albfan
Feb 5 at 14:46
2
2
Thank you. I will learn a lot from your anwser and now I see that terms werent completely clear to me. Is there any shorter way? Maybee a command already integrated in the Linux itself?
– 71GA
Jul 17 '14 at 8:43
Thank you. I will learn a lot from your anwser and now I see that terms werent completely clear to me. Is there any shorter way? Maybee a command already integrated in the Linux itself?
– 71GA
Jul 17 '14 at 8:43
Nope. If there was a pre-existing command, I would happily recommend it.
– Patrick
Jul 17 '14 at 17:07
Nope. If there was a pre-existing command, I would happily recommend it.
– Patrick
Jul 17 '14 at 17:07
Suggest to change /bin/bash to /bin/sh and remove the ( ) for max compatibility
– albfan
Feb 5 at 14:46
Suggest to change /bin/bash to /bin/sh and remove the ( ) for max compatibility
– albfan
Feb 5 at 14:46
add a comment |
You can use this command to explore your device if connected to usb0
:
udevadm info -a -p $(udevadm info -q path -n /dev/ttyUSB0)
The top answer didn't work on my machine, but this worked great. Thanks!
– johnny_boy
Jun 19 '17 at 14:45
add a comment |
You can use this command to explore your device if connected to usb0
:
udevadm info -a -p $(udevadm info -q path -n /dev/ttyUSB0)
The top answer didn't work on my machine, but this worked great. Thanks!
– johnny_boy
Jun 19 '17 at 14:45
add a comment |
You can use this command to explore your device if connected to usb0
:
udevadm info -a -p $(udevadm info -q path -n /dev/ttyUSB0)
You can use this command to explore your device if connected to usb0
:
udevadm info -a -p $(udevadm info -q path -n /dev/ttyUSB0)
edited May 7 '15 at 7:45
cuonglm
105k25209307
105k25209307
answered May 7 '15 at 7:38
NaveenNaveen
9111
9111
The top answer didn't work on my machine, but this worked great. Thanks!
– johnny_boy
Jun 19 '17 at 14:45
add a comment |
The top answer didn't work on my machine, but this worked great. Thanks!
– johnny_boy
Jun 19 '17 at 14:45
The top answer didn't work on my machine, but this worked great. Thanks!
– johnny_boy
Jun 19 '17 at 14:45
The top answer didn't work on my machine, but this worked great. Thanks!
– johnny_boy
Jun 19 '17 at 14:45
add a comment |
You could try something like this below.
echo -n "/dev/"; dmesg | grep tty|grep USB|rev|awk 'print $1'|rev
ow my eyes!dmesg | awk '/tty/ && /USB/ print "/dev/"$1'
(The equivalent but clean...but still strange code; why not just ttyUSB together? and what was 2x rev for?)
– Peter
Nov 13 '17 at 11:40
add a comment |
You could try something like this below.
echo -n "/dev/"; dmesg | grep tty|grep USB|rev|awk 'print $1'|rev
ow my eyes!dmesg | awk '/tty/ && /USB/ print "/dev/"$1'
(The equivalent but clean...but still strange code; why not just ttyUSB together? and what was 2x rev for?)
– Peter
Nov 13 '17 at 11:40
add a comment |
You could try something like this below.
echo -n "/dev/"; dmesg | grep tty|grep USB|rev|awk 'print $1'|rev
You could try something like this below.
echo -n "/dev/"; dmesg | grep tty|grep USB|rev|awk 'print $1'|rev
edited Dec 8 '16 at 7:06
DarkHeart
3,52632441
3,52632441
answered Dec 8 '16 at 6:40
VeggieVampireVeggieVampire
234
234
ow my eyes!dmesg | awk '/tty/ && /USB/ print "/dev/"$1'
(The equivalent but clean...but still strange code; why not just ttyUSB together? and what was 2x rev for?)
– Peter
Nov 13 '17 at 11:40
add a comment |
ow my eyes!dmesg | awk '/tty/ && /USB/ print "/dev/"$1'
(The equivalent but clean...but still strange code; why not just ttyUSB together? and what was 2x rev for?)
– Peter
Nov 13 '17 at 11:40
ow my eyes!
dmesg | awk '/tty/ && /USB/ print "/dev/"$1'
(The equivalent but clean...but still strange code; why not just ttyUSB together? and what was 2x rev for?)– Peter
Nov 13 '17 at 11:40
ow my eyes!
dmesg | awk '/tty/ && /USB/ print "/dev/"$1'
(The equivalent but clean...but still strange code; why not just ttyUSB together? and what was 2x rev for?)– Peter
Nov 13 '17 at 11:40
add a comment |
Perhaps you would like to know just the path to USB-serial adapter that connected last?
dmesg | awk '/tty/ && /USB/ print "/dev/"$10'|tail -1
add a comment |
Perhaps you would like to know just the path to USB-serial adapter that connected last?
dmesg | awk '/tty/ && /USB/ print "/dev/"$10'|tail -1
add a comment |
Perhaps you would like to know just the path to USB-serial adapter that connected last?
dmesg | awk '/tty/ && /USB/ print "/dev/"$10'|tail -1
Perhaps you would like to know just the path to USB-serial adapter that connected last?
dmesg | awk '/tty/ && /USB/ print "/dev/"$10'|tail -1
answered Jul 10 '18 at 13:40
HRmeteohubHRmeteohub
111
111
add a comment |
add a comment |
Assuming that you know what the device you plugged in is, in 14.04 Ubuntu, at least, there is the command usb-devices
that you can look through and find the information:
$ usb-devices
T: Bus=01 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=480 MxCh= 3
D: Ver= 2.00 Cls=09(hub ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
P: Vendor=1d6b ProdID=0002 Rev=04.04
S: Manufacturer=Linux 4.4.0-131-generic ehci_hcd
S: Product=EHCI Host Controller
S: SerialNumber=0000:00:1a.0
C: #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr=0mA
I: If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub
And the first line lists bus and port, as well as the device number that lsusb
gives.
add a comment |
Assuming that you know what the device you plugged in is, in 14.04 Ubuntu, at least, there is the command usb-devices
that you can look through and find the information:
$ usb-devices
T: Bus=01 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=480 MxCh= 3
D: Ver= 2.00 Cls=09(hub ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
P: Vendor=1d6b ProdID=0002 Rev=04.04
S: Manufacturer=Linux 4.4.0-131-generic ehci_hcd
S: Product=EHCI Host Controller
S: SerialNumber=0000:00:1a.0
C: #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr=0mA
I: If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub
And the first line lists bus and port, as well as the device number that lsusb
gives.
add a comment |
Assuming that you know what the device you plugged in is, in 14.04 Ubuntu, at least, there is the command usb-devices
that you can look through and find the information:
$ usb-devices
T: Bus=01 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=480 MxCh= 3
D: Ver= 2.00 Cls=09(hub ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
P: Vendor=1d6b ProdID=0002 Rev=04.04
S: Manufacturer=Linux 4.4.0-131-generic ehci_hcd
S: Product=EHCI Host Controller
S: SerialNumber=0000:00:1a.0
C: #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr=0mA
I: If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub
And the first line lists bus and port, as well as the device number that lsusb
gives.
Assuming that you know what the device you plugged in is, in 14.04 Ubuntu, at least, there is the command usb-devices
that you can look through and find the information:
$ usb-devices
T: Bus=01 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=480 MxCh= 3
D: Ver= 2.00 Cls=09(hub ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
P: Vendor=1d6b ProdID=0002 Rev=04.04
S: Manufacturer=Linux 4.4.0-131-generic ehci_hcd
S: Product=EHCI Host Controller
S: SerialNumber=0000:00:1a.0
C: #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr=0mA
I: If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub
And the first line lists bus and port, as well as the device number that lsusb
gives.
answered Aug 22 '18 at 17:09
GertlexGertlex
111
111
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f144029%2fcommand-to-determine-ports-of-a-device-like-dev-ttyusb0%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What are you trying to accomplish? Do you want to associate a device in
/dev
with an entry inlsusb
? Or do you want to list all devices in/dev
that are derived from a physical USB device? Or are you just wantingls /dev/ttyUSB*
?– Patrick
Jul 11 '14 at 17:00
I just need a command which will give me port of a device and will not push me to disconnect and reconnect my devices.
ls /dev/ttyUSB*
will only list maybee 10 ports but from this list I cannot tell which one is for my device.– 71GA
Jul 15 '14 at 16:41
I found this to be helpful. lsusb -v
– tjaart55
yesterday