Linux blkid Command

Linux 命令大全Linux Command Reference


What is the blkid command?

blkidIt is a utility in Linux systems for identifying block device attributes. It can display the file system type, UUID (Universally Unique Identifier), label (LABEL), and other related information of block devices (such as hard disks, partitions, USB devices, etc.).

Main Features

  • Identify the file system type of storage devices
  • Get the device's unique identifier (UUID)
  • View the partition label (LABEL) of a device
  • Display the size and other attributes of block devices

Basic Syntax

blkid [选项] [设备...]

Common Options and Parameter Descriptions

Option Description
-c <缓存文件> Specify the cache file (default is /etc/blkid.tab)
-g Collect garbage data into the cache file
-o <格式> Specify the output format (full, value, list, device, udev)
-p Low-level superblock probing (bypasses cache)
-s <标签> Display information for the specified tag (such as UUID, TYPE, LABEL, etc.)
-U <UUID> Find the corresponding device by UUID
-L <标签> Find the corresponding device by LABEL
-i Display information about I/O limits
-h Display help information
-V Display version information

Usage Examples

1. View All Block Device Information

Example

sudo blkid

Example output:

/dev/sda1: UUID=&quot;5a3a1e7b-1a2b-4c3d-8e9f-0a1b2c3d4e5f&quot; TYPE=&quot;ext4&quot; 
/dev/sda2: UUID=&quot;6b4c5d6e-7f8g-9h0i-1j2k-3l4m5n6o7p8q&quot; TYPE=&quot;swap&quot; 
/dev/sdb1: LABEL=&quot;DATA&quot; UUID=&quot;9a8b7c6d-5e4f-3g2h-1i0j-9k8l7m6n5o4p&quot; TYPE=&quot;xfs&quot;

2. View Specific Device Information

Example

sudo blkid /dev/sda1

Example output:

/dev/sda1: UUID=&quot;5a3a1e7b-1a2b-4c3d-8e9f-0a1b2c3d4e5f&quot; TYPE=&quot;ext4&quot;

3. Display Only UUID Information

Example

sudo blkid -s UUID -o value /dev/sda1

Example output:

5a3a1e7b-1a2b-4c3d-8e9f-0a1b2c3d4e5f

4. Find Device by UUID

Example

sudo blkid -U 5a3a1e7b-1a2b-4c3d-8e9f-0a1b2c3d4e5f

Example output:

/dev/sda1

5. Use List Format Output

Example

sudo blkid -o list

Example output:

device     fs_type label    mount point    UUID
-------------------------------------------------------------------------------
/dev/sda1  ext4             /              5a3a1e7b-1a2b-4c3d-8e9f-0a1b2c3d4e5f
/dev/sda2  swap             [SWAP]         6b4c5d6e-7f8g-9h0i-1j2k-3l4m5n6o7p8q
/dev/sdb1  xfs    DATA      /mnt/data      9a8b7c6d-5e4f-3g2h-1i0j-9k8l7m6n5o4p

Practical Application Scenarios

1. Mount Using UUID in fstab

When editing/etc/fstabthe file, using UUID is more reliable than device names because device names may change:

Example

UUID=5a3a1e7b-1a2b-4c3d-8e9f-0a1b2c3d4e5f / ext4 defaults 0 1

2. Identify Devices in Automation Scripts

In scripts, you can use blkid to identify a specific device:

Example

#!/bin/bash

DATA_PARTITION=$(sudo blkid -L "DATA")
if [ -n "$DATA_PARTITION" ]; then
    echo "Found data partition:$DATA_PARTITION"
    mount $DATA_PARTITION /mnt/data
else
    echo "Data partition not found"
fi

3. Detect Unmounted File Systems

Example

sudo blkid | grep -v "$(mount | awk '{print $1}')"

FAQ

Q1: Why is sudo needed to run blkid?

A: Ordinary users may not have permission to access certain device information. Using sudo ensures that complete device information is obtained.

Q2: What is the difference between blkid and lsblk?

A:

  • lsblkMainly displays the hierarchical structure and basic information of block devices (name, size, mount point, etc.)
  • blkidFocuses on displaying detailed file system-related information (UUID, TYPE, LABEL, etc.)

Q3: How to update the blkid cache?

A: You can update the cache with the following command:

Example

sudo blkid -g

Or use low-level probing to completely bypass the cache:

Example

sudo blkid -p

Summary

blkidThe command is a very useful tool in Linux system administration, especially when dealing with storage devices and file systems. After studying this article, you should be able to:

  1. Understand the basic functions and syntax of blkid
  2. Use various options to obtain specific device information
  3. Apply the blkid command in real-world scenarios
  4. Solve common problems related to device identification

Mastering the blkid command will help you manage storage devices in Linux systems more effectively.

Linux 命令大全Linux Command Reference

Other Extensions