Unmount all USB devices with a hotkey

terminalI was looking for a solution to unmount all attached USB pen drives or disks with a single keyboard shortcut on Ubuntu 13.04. On googling a bit I found this thread. However, the limitation of the script is it doesn’t take care of USB hard disks. So I modified it a little and saved it in an executable script /usr/bin/remusb. You can download it from here.

#!/bin/bash

command_exists () {
    type "$1" &> /dev/null
}

CMD=""

if command_exists udevadm; then
        CMD="$(which udevadm) info -q all -n"
fi

if command_exists udevinfo; then
        CMD="$(which udevinfo) -q all -n"
fi

echo "The udevinfo command for this system is $CMD"

for disk in /dev/sd[a-z][1-9]
do
        DISK=$($CMD $disk | grep ID_BUS)
        if [[ "$DISK" == *usb ]]; then
                echo "$disk is USB pen drive, now unmounting.."
                umount $disk
        else
                DISK=$($CMD $disk | grep -w ID_PATH)
                if [[ "$DISK" == *usb* ]]; then
                        echo "$disk is USB hard disk, now unmounting.."
                        umount $disk
                fi
        fi
done

To assign a keyboard shortcut I visited Keyboard settings ▸ Shortcuts ▸ +. Added the description and the command as sudo remusb (in my system sudo runs without password, check this post). Assigned a keyboard shortcut for the command and I’m done. Now I can easily unmount all the attached USB devices from my laptop at once!

Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s