blkid useful examples

Similar to lsblk, the blkid is a command-line utility to locate/print block device attributes. It also can be used to search a blockdev by its parameters Here are some blkid command examples on linux.

Display each listed block device attributes

By default, the blkid command displays available attributes such as its universally unique identifier (UUID), file system type (TYPE), or volume label (LABEL).

# blkid
/dev/sda: UUID="a4dc8beb-1f79-4a54-883c-26bad0570794" TYPE="xfs" LABEL="/dcs07_l4_5"
/dev/sdb1: UUID="e10dc5c0-c27a-4617-8945-daab6d597137" TYPE="ext4"
/dev/sdb2: UUID="8e37d096-4494-4a31-80df-e74b6a8c26e3" TYPE="ext4"
/dev/sdb3: UUID="8b13596f-3647-4289-b8ab-89ce31719e42" TYPE="swap"
/dev/sdb5: UUID="f2b734b2-2e14-44cd-a1c8-2421b7e4d1b7" TYPE="ext4"
/dev/sdb6: UUID="3166aa04-d05e-4b32-a306-939679882090" TYPE="ext4"
/dev/sdb7: UUID="27043be4-5a02-44e3-ad6d-eacc4823c81f" TYPE="ext4"
/dev/sdb8: UUID="63d12776-b54a-454e-b721-e92526980b3f" TYPE="ext4"
/dev/sdb9: UUID="4442fda3-7149-4f78-b114-ac8820c51549" TYPE="ext4"
/dev/sdc1: LABEL="/data" UUID="d21aebfe-716e-11e2-97f7-001a64633f00" TYPE="ext4"
/dev/mapper/dcs07_l4_5: LABEL="/dcs07_l4_5" UUID="a4dc8beb-1f79-4a54-883c-26bad0570794" TYPE="xfs"

As it says in its own man page

The  blkid  program  is  the  command-line  interface to working with libblkid(3) library.
It can determine the type of content (e.g.filesystem, swap) a block device holds, and also
attributes (tokens, NAME=value pairs) from the content metadata (e.g. LABEL  or  UUID fields).

blkid display a single device attribute

the blkid command with a device name display only this device attributes

# blkid /dev/sda
/dev/sda: UUID="a4dc8beb-1f79-4a54-883c-26bad0570794" TYPE="xfs" LABEL="/dcs07_l4_5"

blkid displays block device i/o limits

With '-i' option, the blkid displays the I/O limits on a particular block device

# blkid -i /dev/sdc1
MINIMUM_IO_SIZE=512
PHYSICAL_SECTOR_SIZE=512
LOGICAL_SECTOR_SIZE=512

 blkid displays block deviceaddition information

The -p option will display additional information, extract the information from the superblock

# blkid -p /dev/sdc1
/dev/sdc1: LABEL="/data" UUID="d21aebfe-716e-11e2-97f7-001a64633f00" VERSION="1.0" TYPE="ext4" USAGE="filesystem"

Combine with '-o' option,

# blkid -po udev /dev/sdc1
ID_FS_LABEL=/data
ID_FS_LABEL_ENC=data
ID_FS_UUID=d21aebfe-716e-11e2-97f7-001a64633f00
ID_FS_UUID_ENC=d21aebfe-716e-11e2-97f7-001a64633f00
ID_FS_VERSION=1.0
ID_FS_TYPE=ext4
ID_FS_USAGE=filesystem

Or.

# blkid -po full /dev/sdc1/dev/sdc1
LABEL="/data"
UUID="d21aebfe-716e-11e2-97f7-001a64633f00"
VERSION="1.0"
TYPE="ext4"
USAGE="filesystem"

You may noticed that there was a 'full' option added to '-o' option, here is more detail about -o option

-o format
   Display blkid's output using the specified format.  The format parameter may be:
   full   print all tags (the default)
   value  print the value of the tags
   list   print the devices in a user-friendly format,
this output format is unsupported for low-level probing (-p or -i)

   device print the device name only, this output format is always enabled for -L and -U options
   udev   print key="value" pairs for easy import into the udev environment
   export print key=value pairs for easy import into the environment. 
This output format is automatically enabled when I/O Limits
(-i option) are requested.

blkid search a device by UUID

To search a block device by UUID, run

# blkid -U 8b13596f-3647-4289-b8ab-89ce31719e42
/dev/sdb3

blkid search a device by device label

To search a device by its label, run

# blkid -L /data
/dev/sdc1

blkid search devices by filesystem type or other parameters

By filesystem type:

# blkid -l -t TYPE=ext4
/dev/sdb1: UUID="e10dc5c0-c27a-4617-8945-daab6d597137" TYPE="ext4"

By UUID, run:

# blkid -l -t UUID=d21aebfe-716e-11e2-97f7-001a64633f00
/dev/sdc1: LABEL="/data" UUID="d21aebfe-716e-11e2-97f7-001a64633f00" TYPE="ext4"

By type, run

# blkid -l -t TYPE=swap
/dev/sdb3: UUID="8b13596f-3647-4289-b8ab-89ce31719e42" TYPE="swap"

Note: When you specify TYPE in -t option, it only shows the first device which matchs, it doesn't make sense to me.

Note:

One thing need to mention is that -p and -i are low level probing options, which they bypass cachefiles, default is /etc/blkid/blkid.tab, so when you use low level probing options, you can also specify other options like -s and -u to restrict probing functions

 Low-level probing options:
  -p          low-level superblocks probing (bypass cache)

  -i          gather information about I/O limits
  -S <bytes>  overwrite device size
  -O <bytes>  probe at the given offset
  -u <list>   filter by "usage" (e.g. -u filesystem,raid)
     Restrict probing functions to defined (comma separated) list of "usage" types.
  Supported usage types  are:  filesystem,  raid,
crypto and other.
The list can be prefixed with "no" to specify the usage types which should be ignored.

For example:

     blkid -p -u filesystem,other /dev/sda1
     probes for all filesystems and others (e.g. swap) formats, and
     blkid -p -u noraid /dev/sda1
     probes for all supported formats exclude RAIDs. This option is useful with -p only.