Introduction
Detecting the presence of a USB device on a Linux machine is a common task, especially in scripting and automation scenarios. Whether you want to perform specific actions when a USB device is plugged in or simply check its existence, Linux provides several commands and tools to accomplish this. In this article, we will explore different methods to check whether a USB device is present on a Linux machine.
1. Using lsusb
Command
The lsusb
command is a powerful utility that lists USB devices connected to the system. It displays detailed information about each USB device, including the vendor ID, product ID, and device description.
1.1. Checking for Specific USB Device
To check whether a specific USB device is present, you can use the grep
command to filter the output of lsusb
. First, you need to know the vendor ID and product ID of the USB device.
- Connect the USB device to the Linux machine.
- Open a terminal and run the following command:
lsusb | grep "<vendor_id>:<product_id>"
Replace <vendor_id>
and <product_id>
with the actual vendor ID and product ID of the USB device. If the device is connected, you will see the relevant line in the output.
1.2. Listing All Connected USB Devices
If you want to list all connected USB devices, you can simply run the lsusb
command without any filters:
lsusb
This will display detailed information about all USB devices connected to the Linux machine.
2. Using udevadm
Command
The udevadm
command is another useful utility for managing and inspecting device information in Linux.
2.1. Checking for Specific USB Device
To check whether a specific USB device is present using udevadm
, you can filter the output by the vendor and product attributes.
- Connect the USB device to the Linux machine.
- Open a terminal and run the following command:
udevadm info -q property -n /dev/<device_name> | grep ID_VENDOR_ID="<vendor_id>" | grep ID_MODEL_ID="<product_id>"
Replace <device_name>
, <vendor_id>
, and <product_id>
with the actual device name (e.g., sdb
for a USB storage device), vendor ID, and product ID of the USB device. If the device is connected, you will see the relevant lines in the output.
2.2. Listing All Connected USB Devices
To list all connected USB devices using udevadm
, you can use the following command:
udevadm info -q property -n /dev/sd*
This will display detailed information about all USB devices connected to the Linux machine.
3. Using lsblk
Command
The lsblk
command is a utility for listing block devices, including USB storage devices.
3.1. Checking for Specific USB Storage Device
To check whether a specific USB storage device is present, you can use the lsblk
command and filter the output using the device name.
- Connect the USB storage device to the Linux machine.
- Open a terminal and run the following command:
lsblk | grep "<device_name>"
Replace <device_name>
with the actual device name (e.g., /dev/sdb
) of the USB storage device. If the device is connected, you will see the relevant line in the output.
3.2. Listing All Connected USB Storage Devices
To list all connected USB storage devices, you can simply run the lsblk
command without any filters:
lsblk
This will display information about all block devices, including USB storage devices, connected to the Linux machine.
4. Automating Actions with USB Device Detection
Now that we have learned how to check for the presence of a USB device on a Linux machine, let’s explore how we can automate actions based on USB device detection. This can be particularly useful for tasks such as automounting USB storage devices, backing up data, or launching specific applications when a USB device is connected.
4.1. Automounting USB Storage Devices
Automatically mounting USB storage devices upon connection is a common requirement. We can achieve this by leveraging the udev
rules and the mount
command.
- Create a new
udev
rule file for automounting USB storage devices. For example, create a file named99-usb-automount.rules
in the/etc/udev/rules.d/
directory:
sudo nano /etc/udev/rules.d/99-usb-automount.rules
- Add the following rule to the file:
ACTION=="add", KERNEL=="sd*[!0-9]", ENV{ID_FS_USAGE}=="filesystem", RUN+="/bin/mount -U $env{ID_FS_UUID}"
- Save and close the file.
This rule checks for any USB storage devices (sd*
) that are added and have a filesystem. When a USB storage device is detected, the rule automatically mounts it using the mount
command.
4.2. Backup Upon USB Device Connection
You can create a script that automatically triggers a backup process when a specific USB device is connected. This can be helpful to ensure your important data is backed up whenever you plug in the USB device.
- Create a backup script. For example, create a file named
backup_script.sh
:
#!/bin/bash
# Add your backup commands here
# For example, rsync or cp commands to backup data to a specified location
- Make the script executable:
chmod +x backup_script.sh
- Create a
udev
rule to trigger the backup script when the specific USB device is connected. Create a new rule file, such as99-usb-backup.rules
:
sudo nano /etc/udev/rules.d/99-usb-backup.rules
- Add the following rule to the file:
ACTION=="add", KERNEL=="sd*[!0-9]", ENV{ID_FS_UUID}=="<your_usb_device_uuid>", RUN+="/path/to/backup_script.sh"
Replace <your_usb_device_uuid>
with the UUID of your specific USB device. You can find the UUID by running lsblk -o name,label,uuid
or blkid
.
- Save and close the file.
Now, whenever you plug in the specified USB device, the udev
rule will trigger the backup script, and your data will be automatically backed up.
4.3. Launching Applications on USB Device Connection
You can also configure Linux to launch specific applications or scripts automatically when a USB device is connected.
- Create a script or application launcher for the desired application. For example, create a file named
launch_app.sh
:
#!/bin/bash
# Add your application launch command here
# For example, to launch a media player, use: vlc /path/to/media
- Make the script executable:
chmod +x launch_app.sh
- Create a
udev
rule to trigger the application when the specific USB device is connected. Create a new rule file, such as99-usb-launch-app.rules
:
sudo nano /etc/udev/rules.d/99-usb-launch-app.rules
- Add the following rule to the file:
ACTION=="add", KERNEL=="sd*[!0-9]", ENV{ID_FS_UUID}=="<your_usb_device_uuid>", RUN+="/path/to/launch_app.sh"
Replace <your_usb_device_uuid>
with the UUID of your specific USB device.
- Save and close the file.
Now, whenever you plug in the specified USB device, the udev
rule will trigger the application launcher, and your desired application will be automatically launched.
5. Conclusion
Automating actions based on USB device detection is a powerful capability that Linux provides through udev
rules and various commands. By using udev
rules, we can trigger scripts or applications when specific USB devices are connected. This allows us to perform tasks such as automounting USB storage devices, backing up data, or launching applications automatically, streamlining our workflows and improving productivity.
In this article, we explored how to automate actions with USB device detection using udev
rules and commands like lsusb
, udevadm
, and lsblk
. We discussed automounting USB storage devices, performing backups upon USB device connection, and launching applications automatically. By applying these techniques, you can create efficient and automated workflows for managing USB devices on your Linux machine.