blkid Command in Linux



The blkid is a robust command line utility used to locate or print block device attributes. A block device can be a solid-state drive (SSD), a hard drive, or removable storage device like a USB drive.

The blkid command can identify what kind of data a block device contains. This includes −

  • Filesystem − Types like ext4, NTFS, FAT32, etc.
  • Swap − Used for swap space in Linux.

When you run blkid, it scans the block devices and reads their metadata to determine the type of content they hold.

Block devices also have metadata that includes various attributes. These attributes are presented as tokens in the form of NAME=value pairs. Some common attributes include −

  • LABEL − A human-readable name assigned to the filesystem
  • UUID − A unique identifier for the filesystem
  • TYPE − The type of filesystem (e.g., ext4, swap)

The blkid command reads the block device's superblock or other metadata structures to extract this information. This allows you to quickly identify and manage your storage devices based on their attributes.

The blkid command is part of the util-Linux package. To get started with this command, you'll need a Linux terminal with root or sudo privileges.

Table of Contents

Here is a comprehensive guide to the options available with the blkid command −

Syntax of blkid Command

The following is the general syntax for the blkid command −

blkid [OPTIONS] [DEVICE_NAME]

Where −

  • OPTIONS − refers to different flags you can use to modify the behavior of the blkid command.
  • DEVICE NAME − name of the device to be searched.

blkid Command Options

The following is a list of different options you can use with the blkid Command −

Options Description
-c Read from a specified cache file instead of the default /etc/blkid/blkid.tab. If you want to start with a clean cache, use /dev/null.
-h Display a usage message and exit.
-l

Look up the device that matches the search parameter specified with the -t option. This is more efficient for systems with many disks as it avoids unnecessary revalidation.

If this option is not specified, blkid will use a less efficient approach, which allows blkid to print all of the devices that match the search parameter.

Use this for specific tag searches like LABEL=data_vol or UUID=e280469a-d06f-4c0b-b068-44f3b576029e.

-o Display blkid’s output using the specified format. The format parameter may be full, (the default), value, (only print the value of any tags printed by blkid) or device (only print the device name).
-s Show only the tags for each specified device that match the given tag. You can specify multiple -s options. If no tag is specified, all tokens are shown. To refresh the cache without showing tokens, use -s none.
-t Search for block devices with tokens named NAME that have the value value, and display any devices found. Common values for NAME include TYPE, LABEL, and UUID. If no devices are specified on the command line, all block devices will be searched; otherwise, only the specified devices will be searched.
-v Display the version number of blkid and exit.
-w <writecachefile>

Write the device cache to writecachefile instead of the default /etc/blkid/blkid.tab.

If you don’t want to save the cache to the default file, specify /dev/null. If not specified, it will use the same file as given by the -c option.

<device> Display tokens from only the specified device(s). You can specify multiple devices on the command line. If none are given, all recognized devices in /proc/partitions are shown.

Examples of blkid Command in Linux

blkid has two main forms of operation: displaying NAME=value pairs for one or more specified devices or searching for a device with a specific NAME=value pair.

  • Display Information about all Block Devices
  • Display Information about a Specific Block Device
  • Format the Output to Show Only Device Names
  • Write the Device Cache to a Specific File
  • Search for Block Devices with a Specific Label
  • Show Only the UUID of a Specific Device
  • Display the Version of blkid
  • Search Only the UUID of a Specific Device
  • Display Information about a Specific Block Device without Encoding Non-printing Characters
  • Remove Devices that no Longer Exist from the Cache
  • List All Known Filesystems and RAIDs
  • Probe for Specific Filesystem Types on a Device
  • Display Only the Device Name for a Specific Block Device

Display Information about all Block Devices

To display information about all block devices along with their attributes, run the following command −

blkid
Information all blkid Block Devices

Display Information about a Specific Block Device

To display information about a specific block device using the blkid command, you can specify the device name as an argument −

blkid /dev/sda2
Information Specific blkid Block Device

Format the Output to Show Only Device Names

To format the output to show only the names of the block devices, use the following command −

blkid -o device
Format Output to Show Device Names

Write the Device Cache to a Specific File

To write the device cache to a specified file instead of the default cache file, use the following command −

blkid -w /tmp/my_cachefile

Replace /tmp/my_cachefile with the actual path where you want to save the cache file.

Write Device Cache to Specific File

Search for Block Devices with a Specific Label

To search for block devices that have the label data_vol and display their information, use the following command −

blkid -t LABEL=data_vol

This command searches for block devices with the label data_vol and prints their details. If no output is returned, it means no block devices with that label were found.

Search Block Devices with Specific Label

Show Only the UUID of a Specific Device

To show only the UUID of a specific device, use the following command −

blkid -s UUID -o value /dev/sda2
Show Only UUID of Specific Devic

Display the Version of blkid

To show the version number of the blkid command, you can use the following command −

blkid -v
Display Version of blkid

Search Only the UUID of a Specific Device

To search for a block device with the specified UUID, you can use the following syntax −

blkid -t UUID=b6d60dfd-5aec-4e8a-b0ce-cf8226488c9a
Search Only UUID of Specific Device

Display Information about a Specific Block Device without Encoding Non-printing Characters

To show the attributes of a block device without encoding non-printing characters, you can use the "-d" flag as shown −

blkid -d /dev/sda2
Info of Specific Block Device without Encoding

Remove Devices that no Longer Exist from the Cache

To refresh the information about block devices stored in the cache, you can use the following command −

blkid -g

Running this command doesn’t produce any output, but it updates the cache with the latest block device information.

Remove Devices from blkid Cache

List All Known Filesystems and RAIDs

To list all known filesystems and RAID types, you can use the blkid command with the "-k" flag −

blkid -k
List All Known Filesystems and RAIDs

Probe for Specific Filesystem Types on a Device

To probe a specific device for filesystem types, you can use the following command −

blkid -p -u filesystem /dev/sda2

This command probes /dev/sdb1 for all filesystem types. The "-p" option tells blkid to probe the device, and "-u" filesystem specifies that it should look for filesystem types.

Probe Specific Device for Filesystem Types

Display Only the Device Name for a Specific Block Device

To print only the device name of the block device, you can use the following command −

blkid -o device /dev/sda2

The "-o" device option specifies that the output should be the device name only.

Display Device Name for Specific Block Device

Conclusion

In this tutorial, we covered the blkid command in Linux, which is a vital tool for identifying and displaying attributes of block devices, including hard drives and USBs.

The examples given in this tutorial should give you a broader understanding of how to use the blkid command for various tasks related to block devices.

Advertisements