blkid

2024-06-30

What is blkid?

blkid (block ID) is a powerful command-line utility that queries the kernel’s block device information. It’s specifically designed to retrieve the UUID (Universally Unique Identifier) and other identifying attributes of block devices, such as hard drives, SSDs, USB drives, and partitions. This information is essential for tasks like:

Basic Usage and Examples

The simplest way to use blkid is to run it without any arguments:

blkid

This command displays a list of all block devices detected by the system, along with their UUIDs, TYPE (filesystem type), and other relevant attributes. For instance, the output might look like this:

/dev/sda1: UUID="a1b2c3d4-e5f6-7890-1234-567890abcdef" TYPE="ext4"
/dev/sda2: UUID="0000-0000" TYPE="swap"
/dev/sdb1: UUID="f0e9d8c7-b6a5-4321-8765-4321fedcba98" TYPE="vfat"

This shows that /dev/sda1 is formatted with ext4, /dev/sda2 is a swap partition, and /dev/sdb1 is formatted with the FAT filesystem (vfat).

Specifying Devices

You can target specific devices by providing their device names as arguments:

blkid /dev/sda1

This will only show information for the /dev/sda1 partition.

Extracting Specific Information

blkid allows you to extract specific information using the -o option. For example, to only get the UUID:

blkid -o value -s UUID /dev/sda1

This will output only the UUID of /dev/sda1. Similarly, you can obtain the TYPE:

blkid -o value -s TYPE /dev/sda1

Handling Multiple Devices and Output Formatting

For more complex scenarios, you might want to process the output of blkid. This can be easily done by piping the output to other commands like grep or awk. For instance, to find all partitions with the ext4 filesystem:

blkid | grep "TYPE=\"ext4\""

To extract only the UUIDs of all ext4 partitions and print them one per line:

blkid | grep "TYPE=\"ext4\"" | awk '{print $2}' | sed 's/UUID="//;s/"//g'

This uses awk to extract the second field (the UUID), and sed to remove the surrounding quotes from the UUIDs.

Exploring Advanced Options

blkid provides many other options for fine-grained control over its output. Consult the man blkid page for a detailed list of options and their usage. Understanding these options is important for adapting blkid to various storage management tasks. For example, the -c option allows specifying an alternative configuration file, useful for managing multiple block device databases. Experimentation and exploring the man page are highly encouraged to master the full potential of this versatile command.