List all block devices

This post doesn’t show you how to do anything very useful, unless you are as bad at remembering device names as I am, but I thought it worth posting because of the way it solves the problem, which I quite like.

There have been a few times recently when I’ve wanted to know what mass storage devices there are on a linux system. This is easy enough to figure out by using ‘find’ to return all the paths containing ‘stat’ in ‘/sys’.

$ find /sys -name ‘stat’|grep ‘stat’
/sys/devices/pci  …  /host7/target7:0:0/7:0:0:0/block/sdd/stat
/sys/devices/pci  …  /host7/target7:0:0/7:0:0:1/block/sde/stat
/sys/devices/pci  …  /host7/target7:0:0/7:0:0:2/block/sdf/stat
/sys/devices/pci  …  /host1/target1:0:0/1:0:0:0/block/sda/sda1/stat
/sys/devices/pci  …  /host1/target1:0:0/1:0:0:0/block/sda/sda2/stat
/sys/devices/pci  …  /host1/target1:0:0/1:0:0:0/block/sda/sda3/stat
/sys/devices/pci  …  /host1/target1:0:0/1:0:0:0/block/sda/sda4/stat
/sys/devices/pci  …  /host1/target1:0:0/1:0:0:0/block/sda/sda5/stat
/sys/devices/pci  …  /host1/target1:0:0/1:0:0:0/block/sda/sda6/stat
/sys/devices/pci  …  /host1/target1:0:0/1:0:0:0/block/sda/sda7/stat
/sys/devices/pci  …  /host1/target1:0:0/1:0:0:0/block/sda/sda8/stat
/sys/devices/pci  …  /host1/target1:0:0/1:0:0:0/block/sda/stat
/sys/devices/pci  …  /host4/target4:0:0/4:0:0:0/block/sr0/stat
/sys/devices/pci  …  /host5/target5:0:0/5:0:0:0/block/sdb/sdb1/stat
/sys/devices/pci  …  /host5/target5:0:0/5:0:0:0/block/sdb/stat
/sys/devices/pci  …  /host6/target6:0:0/6:0:0:0/block/sdc/sdc1/stat
/sys/devices/pci  …  /host6/target6:0:0/6:0:0:0/block/sdc/sdc2/stat
/sys/devices/pci  …  /host6/target6:0:0/6:0:0:0/block/sdc/stat

(And yes, like a lot of modern machines mine does have an awful lot of storage devices – and the first three don’t actually exist!)

Great, this command does just what I wanted, except that I what I needed was to be able to enumerate each device name in a script. Simple I thought, the device names are the penultimate field in each file path, I’ll just use ‘cut’ to extract the second field from the right and I’m sorted….

Unfortunately though that sounds like a good idea it wont work, as ‘cut’ can only count delimiters from the left and trying to use a negative value to count from the right doesn’t work!

$ find /sys -name ‘stat’|grep ‘stat’|cut -f -2 -d’/’
find: `/sys/fs/fuse/connections/33′: Permission denied
find: `/sys/kernel/debug’: Permission denied

So if ‘cut’ can only count from the left the field I wanted was going to have to be the second field in the string, but how?

The answer lies in the fact that when finding the field we don’t care how readable the string is, we just need the delimiters to be in the right place. If we reverse the order of all the letters in the string then the second field from the right becomes the second field from the left and we can extract it using ‘cut’, then in order to make the output readable, we just have to reverse it again.

In addition in the example below I’ve also used ‘sort’ to list the device names in order.

$ find /sys -name ‘stat’|& grep ‘stat’|rev|cut -f 2 -d’/’|rev|sort -V

Now I don’t have any excuses for not being able to remember the device names of the partitions on my Raspberry Pi anymore!

$ find /sys -name ‘stat’|& grep ‘stat’|rev|cut -f 2 -d’/’|rev

Raspberry Pi is a trademark of the Raspberry Pi Foundation

This entry was posted in Debian, Programming, Raspbian, Ubuntu and tagged . Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your 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