Umount All USB

This is another automation recipe for unmounting all USB devices. The idea is to loop through USB devices for gathering USB device and partitions name and then unmount them one by one.

Basic Loop

This is the basic form:

$!/bin/sh
for usb_device in /dev/disk/by-id/usb-*; do
        [ -e "$usb_device ] || continue
        target="$(readlink -f "$usb_device")"
        grep -q ^"$target" /proc/mounts && umount "$target" 2> /dev/null
done

With Desktop Notification

This is the upgraded version with desktop notification. You need the notify-send package of your operating system:

$!/bin/sh
for usb_device in /dev/disk/by-id/usb-*; do
        [ -e "$usb_device ] || continue
        target="$(readlink -f "$usb_device")"
        grep -q ^"$target" /proc/mounts && umount "$target" 2> /dev/null
done

if [ ! "$(which notify-send)" = "" ]; then
        notify-send \
                --urgency=normal \
                --expire-time=20000 \
                "You may now remove ALL your device"
fi

That's all about automating USB dismounts.