Imagine a situation where the system unit of the computer is located in a hard-to-reach place. And it is impossible to remove the device from the USB port. Then you can try to disconnect and reconnect all USB devices programmatically.

To accomplish this task, we need to write a small program using bash that will do all the work for us.

  1. First, let's create a file and open it in a text editor.
    nano usb.sh
  2. Paste in the newly created script, the code below.
    #!/bin/bash
    for i in /sys/bus/pci/drivers/[uoex]hci_hcd/*:*; do
    [ -e "$i" ] || continue
    echo "${i##*/}" > "${i%/*}/unbind"
    echo "${i##*/}" > "${i%/*}/bind"
    done
    
    How to reconnect all USB devices in Linux
  3. To execute a script, you need to set the executable flag for it.
    sudo chmod ugo+x usb.sh
  4. Run the script (as root).
    sudo ./usb.sh
Note that keyboards, mice, and other input devices are also USB devices. Therefore, during the execution of the script, they will turn off for a while.