This is a simple C script that identifies and displays USB devices connected to a Linux-based system. It reads from the /sys/bus/usb/devices/ directory to list the names of connected USB devices.
The script:
- Opens the
/sys/bus/usb/devices/directory, where USB device information is stored. - Searches for entries containing
:in their names, as these typically correspond to USB devices. - Reads each device's
productfile to retrieve the device name. - Prints each USB device’s name to the console.
- Operating System: Linux-based (the script relies on the
/sysfile system, which is standard in Linux for hardware information). - C Compiler:
gccor any other C compiler.
-
Clone or Download the repository.
-
Compile the Script:
gcc usb_devices.c -o usb_devices
-
Run the Script:
./usb_devices
-
Output:
The script will display a list of USB devices currently connected to your system.
Connected USB devices: Device: USB Keyboard Device: USB Mouse Device: External USB Drive
The list_usb_devices() function opens the /sys/bus/usb/devices/ directory and iterates through each entry:
- It identifies entries with
:in the name, which generally represent USB devices. - For each entry, it attempts to read the
productfile to retrieve the device's name.
The main() function simply calls list_usb_devices() to execute the USB detection logic.
- This script is designed for Linux systems; it will not work on Windows or macOS without significant changes.
- The script reads directly from system files; it may require root permissions to access certain USB device information depending on your system’s configuration.
This project is licensed under the MIT License - see the LICENSE file for details.
Enjoy identifying your connected USB devices!