Linux blkid Command
What is the blkid command
blkid is a utility in Linux used to identify block device attributes. It can display the filesystem type, UUID (universally unique identifier), label (LABEL), and other related information of block devices (such as hard disks, partitions, USB devices, etc.).
Main Function
- Identify the filesystem type of a storage device
- Obtain the unique identifier (UUID) of a device
- View the partition label (LABEL) of a device
- Display the size and other attributes of a block device
Basic Syntax
blkid [选项] [设备...]
Description of common options and parameters
| 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 (bypassing the cache) |
-s <标签> |
Display information for a specified tag (such as UUID, TYPE, LABEL, etc.) |
-U |
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 information for all block devices
Example
sudo
blkid
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 information for a specific device
Example
sudo
blkid
/
dev
/
sda1
Example output:
/dev/sda1: UUID="5a3a1e7b-1a2b-4c3d-8e9f-0a1b2c3d4e5f" TYPE="ext4"
3. Display only UUID information
Example
sudo
blkid
-s
UUID
-o
value
/
dev
/
sda1
Example output:
5a3a1e7b-1a2b-4c3d-8e9f-0a1b2c3d4e5f
4. Find a device by UUID
Example
sudo
blkid
-U
5a3a1e7b-1a2b-4c3d-8e9f-0a1b2c3d4e5f
Example output:
/dev/sda1
5. Output in list format
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
Editing /etc/fstab the file, using a UUID is more reliable than using a device name, because device names can change:
Example
UUID
=5a3a1e7b-1a2b-4c3d-8e9f-0a1b2c3d4e5f
/
ext4 defaults
0
1
2. Identifying 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
"Data partition found: $DATA_PARTITION"
mount
$DATA_PARTITION
/
mnt
/
data
else
echo
"Data partition not found"
fi
3. Detecting unmounted filesystems
Example
sudo
blkid
|
grep
-v
"$(mount | awk '{print $1}')"
Frequently Asked Questions
Q1: Why do you need to use sudo to run blkid?
A: Regular users may not have permission to access certain device information; using sudo ensures you get complete device information.
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 filesystem-related information (UUID, TYPE, LABEL, etc.)
Q3: How do you update the blkid cache?
A: You can use the following command to update the cache:
Example
sudo
blkid
-g
Or bypass the cache entirely using low-level probing:
Example
sudo
blkid
-p
Summary
blkid The command is a very useful tool in Linux system administration, especially when working with storage devices and filesystems. Through 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 more effectively.
Linux Command Reference