blkid Command

Display block device attributes including UUID, filesystem type, labels, and other device information.

Syntax

blkid [OPTIONS] [DEVICE...] blkid -L LABEL blkid -U UUID

The blkid command locates and prints block device attributes, commonly used to identify devices by UUID or label.

Common Options

Option Description
-L LABEL Find device with specified label
-U UUID Find device with specified UUID
-o FORMAT Output format (value, device, list, udev, export)
-s TAG Show only specified tag
-t TYPE Search for specific filesystem type
-c FILE Use cache file
-g Garbage collect cache
-p Low-level probing mode
-i Show I/O limits
-S SIZE Override device size

Basic Usage

List all devices

# Show all block devices blkid # Show specific device blkid /dev/sda1 blkid /dev/sdb1 # Show multiple devices blkid /dev/sda1 /dev/sdb1 /dev/sdc1

Display information about block devices

Find by UUID or label

# Find device by UUID blkid -U 12345678-1234-1234-1234-123456789012 # Find device by label blkid -L "MyDisk" blkid -L "HOME" blkid -L "BACKUP" # Show only UUID blkid -s UUID /dev/sda1 # Show only label blkid -s LABEL /dev/sda1

Locate devices by UUID or filesystem label

Filter by filesystem type

# Show only ext4 filesystems blkid -t TYPE=ext4 # Show only NTFS filesystems blkid -t TYPE=ntfs # Show only swap partitions blkid -t TYPE=swap # Show only FAT32 filesystems blkid -t TYPE=vfat

Filter devices by filesystem type

Output Formats

Different output formats

# Default format blkid /dev/sda1 # Value only blkid -s UUID -o value /dev/sda1 # Device name only blkid -o device -t TYPE=ext4 # List format blkid -o list # Export format (for scripts) blkid -o export /dev/sda1 # Udev format blkid -o udev /dev/sda1

Different output formats for various use cases

Specific attributes

# Show only UUID blkid -s UUID -o value /dev/sda1 # Show only filesystem type blkid -s TYPE -o value /dev/sda1 # Show only label blkid -s LABEL -o value /dev/sda1 # Show multiple attributes blkid -s UUID -s TYPE -s LABEL /dev/sda1 # Show partition UUID (for GPT) blkid -s PARTUUID -o value /dev/sda1

Extract specific device attributes

Practical Examples

System administration

# List all mounted filesystems with UUIDs blkid | grep -E "(ext4|ext3|xfs|btrfs)" # Find all swap partitions blkid -t TYPE=swap # Get UUID for /etc/fstab echo "UUID=$(blkid -s UUID -o value /dev/sda1) /home ext4 defaults 0 2" # Check if device has filesystem if blkid /dev/sdb1 >/dev/null 2>&1; then echo "Device has filesystem" else echo "Device is empty or unformatted" fi

Common system administration tasks

Scripting examples

#!/bin/bash # Get device UUID UUID=$(blkid -s UUID -o value /dev/sda1) echo "Device UUID: $UUID" # Check filesystem type FSTYPE=$(blkid -s TYPE -o value /dev/sda1) case $FSTYPE in ext4|ext3|ext2) echo "Linux filesystem detected" ;; ntfs|vfat) echo "Windows filesystem detected" ;; swap) echo "Swap partition detected" ;; *) echo "Unknown or no filesystem" ;; esac # Find device by label DEVICE=$(blkid -L "BACKUP") if [ -n "$DEVICE" ]; then echo "Backup device found: $DEVICE" fi

Use blkid in shell scripts

Device identification

# Identify USB devices blkid | grep -E "/dev/sd[b-z]" # Show all device information blkid -o list | column -t # Compare device attributes for dev in /dev/sd*1; do if [ -b "$dev" ]; then echo "Device: $dev" blkid -s UUID -s TYPE -s LABEL "$dev" echo "---" fi done # Find devices without labels blkid | grep -v LABEL

Identify and compare block devices

Advanced Usage

Low-level probing

# Low-level device probing blkid -p /dev/sda1 # Show I/O limits blkid -i /dev/sda1 # Probe without cache blkid -p -o udev /dev/sda1 # Override device size blkid -S 1024 /dev/sda1

Advanced probing options

Cache management

# Use specific cache file blkid -c /tmp/blkid.cache # Garbage collect cache blkid -g # Show cache location blkid -c /dev/null -o device # Force cache refresh sudo blkid -g && blkid

Manage blkid cache

Best Practices

blkid Usage Best Practices
  • Use UUID instead of device names in /etc/fstab
  • Check device existence before using blkid
  • Use appropriate output format for scripts
  • Combine with other tools like lsblk for complete information
  • Use labels for easier device identification
  • Regular cache cleanup on systems with changing devices
Important Considerations
  • Permissions - May need root access for some devices
  • Cache - Information may be cached and outdated
  • Device changes - UUIDs change when reformatting
  • Removable media - Devices may not always be present
  • Performance - Probing many devices can be slow

See also