Script to List Removable Block Devices Like USB Keys

February 19, 2014

This is a very simple script that uses sysfs to list removable block devices. This is useful for finding USB devices and the available partitions.

#!/bin/bash
 
for dev in /sys/block/sd*
do
    if [ "$(cat $dev/removable)" == "1" ]
    then
	dev=$(basename $dev)
	echo -n "$dev is a removable block device "
	if [ -d /sys/block/${dev}/${dev}1 ]
	then
	    echo "with partitions " /sys/block/$dev/$dev[0-9]*
	else
	    echo "without partitions"
	fi
    fi
done
 
exit 0

Related Posts