One of the most powerful features of Linux is its total control over hardware events. If you run a headless server, a Raspberry Pi, or a backup NAS, you can configure the operating system to listen for a specific hardware event—like plugging in a specific external hard drive—and instantly trigger a bash script in response.
This allows you to create incredibly satisfying automated workflows, such as plugging in a USB thumb drive and having Linux automatically sync your backup files to it, run a virus scan on it, or pull specific configuration files from it, all without you ever needing to log into the terminal.
This automation is achieved by writing a custom rule for the udev (userspace /dev) device manager.
Step 1: Identify Your USB Drive
First, we need to find the unique Vendor ID and Product ID of your USB drive so Linux knows exactly which device to listen for.
- Open your terminal.
- Plug the USB drive into your Linux machine.
- Run the command:
lsusb - Look for your drive in the list. The output will look something like this:
Bus 002 Device 004: ID 0781:5581 SanDisk Corp. Ultra - Write down the ID block (e.g., 0781:5581). The first four characters are the Vendor ID (
idVendor), and the second four are the Product ID (idProduct).
Step 2: Create the udev Rule
Now we tell the device manager what to do when it sees that specific ID.
- Navigate to the rules directory:
cd /etc/udev/rules.d/ - Create a new rules file using a text editor (requires root privileges). The file should start with a high number (like 99) so it runs after standard system rules:
sudo nano 99-usb-autobackup.rules - Paste the following code into the file, replacing the IDs with the ones you found in Step 1, and the script path with your actual script:
ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="0781", ATTRS{idProduct}=="5581", RUN+="/usr/local/bin/my_backup_script.sh" - Save the file and exit the editor (in nano, press Ctrl+O, Enter, then Ctrl+X).
Step 3: Prepare the Script
The script that udev runs must meet a few strict criteria to work correctly.
- It must be executable: Ensure you run
sudo chmod +x /usr/local/bin/my_backup_script.shon your script. - Use absolute paths: Because udev runs the script in a sterile environment as the root user, standard environmental variables like
$PATHor$HOMEdo not exist. Your script must use absolute, full paths for every command (e.g., use/usr/bin/rsyncinstead of justrsync). - Do not block udev: If your script takes longer than a few seconds to run (like a massive file transfer), udev will forcibly kill it. To prevent this, your script should immediately fork its workload into the background using
&ornohup.
Step 4: Reload udev Rules
Finally, tell the device manager to read the new configuration file you just created.
sudo udevadm control --reload-rules
sudo udevadm trigger
Your automation is now live. Unplug the USB drive and plug it back in. The udev daemon will instantly recognize the hardware IDs and execute your script in the background.