Linux blkid Command
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
Example output:
/dev/sda1: UUID="5a3a1e7b-1a2b-4c3d-8e9f-0a1b2c3d4e5f" TYPE="ext4" /dev/sda2: UUID="6b4c5d6e-7f8g-9h0i-1j2k-3l4m5n6o7p8q" TYPE="swap" /dev/sdb1: LABEL="DATA" UUID="9a8b7c6d-5e4f-3g2h-1i0j-9k8l7m6n5o4p" TYPE="xfs"
2. View Specific Device Information
Example
Example output:
/dev/sda1: UUID="5a3a1e7b-1a2b-4c3d-8e9f-0a1b2c3d4e5f" TYPE="ext4"
3. Display Only UUID Information
Example
Example output:
5a3a1e7b-1a2b-4c3d-8e9f-0a1b2c3d4e5f
4. Find Device by UUID
Example
Example output:
/dev/sda1
5. Use List Format Output
Example
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
2. Identify Devices in Automation Scripts
In scripts, you can use blkid to identify a specific device:
Example
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
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
Or use low-level probing to completely bypass the cache:
Example
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:
- Understand the basic functions and syntax of blkid
- Use various options to obtain specific device information
- Apply the blkid command in real-world scenarios
- Solve common problems related to device identification
Mastering the blkid command will help you manage storage devices in Linux systems more effectively.
Other Extensions
Linux Command Reference