lsblk

The lsblk command is a powerful utility in Linux that provides detailed information about all available or specified block devices. Block devices include hard drives, SSDs, and removable storage devices. Understanding how to use lsblk effectively can help you manage disk partitions, logical volumes, and more.

Basic Usage

Simply running lsblk without any arguments will list all the block devices in a tree-like format:

lsblk

This command outputs a list of block devices, their partitions, and any associated mount points.

Common Fields Displayed by lsblk

The default columns provided by lsblk include:

  • NAME: The name of the device or partition.
  • MAJ:MIN: The major and minor device numbers.
  • RM: Indicates whether the device is removable (1) or not (0).
  • SIZE: The size of the device or partition.
  • RO: Indicates if the device is read-only (1) or read-write (0).
  • TYPE: The type of the device (e.g., disk, part for partition, lvm for logical volume).
  • MOUNTPOINT: The mount point where the filesystem is mounted.

Common Options

  • -a or --all: Lists all devices, including empty ones and devices without partitions.
  lsblk -a
  • -o or --output: Specify the columns to display. You can list multiple columns separated by commas.
  lsblk -o NAME,SIZE,TYPE,MOUNTPOINT
  • -f or --fs: Displays filesystem information like filesystem type (FSTYPE), UUID, and LABEL.
  lsblk -f
  • -d or --nodeps: Shows only top-level devices (disks) without their partitions.
  lsblk -d
  • -r or --raw: Provides output in a raw format without any tree structure or headings.
  lsblk -r
  • -l or --list: Lists devices in a flat format, instead of the tree structure.
  lsblk -l
  • -J or --json: Outputs the information in JSON format, which can be useful for automation and scripting.
  lsblk -J
  • -P or --pairs: Displays output in key=value pairs, useful for scripting.
  lsblk -P
  • -n or --noheadings: Omits the headings from the output.
  lsblk -n

Customizing Output

You can customize the output of lsblk by specifying the columns you are interested in. Some of the available columns include:

  • NAME: Device name.
  • KNAME: Kernel name.
  • MAJ:MIN: Major and minor device numbers.
  • RM: Whether the device is removable.
  • RO: Read-only status.
  • SIZE: Size of the device.
  • FSTYPE: Filesystem type.
  • MOUNTPOINT: Where the device is mounted.
  • UUID: Universally unique identifier of the filesystem.
  • LABEL: Filesystem label.
  • MODEL: Device model.
  • SERIAL: Device serial number.
  • TYPE: Type of the device (disk, part, lvm, etc.).
  • ALIGNMENT: Alignment offset in the device.
  • STATE: The state of the device.
  • OWNER, GROUP, MODE: Ownership and permissions.
  • DISC-ALN: Alignment offset in the disk.
  • DISC-GRAN: Granularity of discard (TRIM) operations.
  • DISC-MAX: Maximum size of discard operations.
  • DISC-ZERO: Whether discard operations zero the data.
  • PHY-SEC: Physical sector size in bytes.
  • LOG-SEC: Logical sector size in bytes.
  • ROTATIONAL: Whether the device is rotational (HDD vs SSD).
  • RQ-SIZE: Request size in the device queue.
  • SCHED: I/O scheduler used.
  • RQ-SIZE: Request size.

For example, to show the device name, size, type, and mount point:

lsblk -o NAME,SIZE,TYPE,MOUNTPOINT

Display Filesystem Information

To display information about the filesystems on the devices:

lsblk -f

This command shows additional columns like FSTYPE (filesystem type), LABEL, UUID, and MOUNTPOINT.

Example Use Cases

  1. Listing Only Physical Disks:
   lsblk -d -o NAME,SIZE,MODEL

This command lists only the disks without their partitions.

  1. Showing Detailed Filesystem Info:
   lsblk -f

This provides an overview of all devices along with their filesystems.

  1. Viewing Device Information Without Headings:
   lsblk -n

Useful in scripts where you don’t need column headers.

  1. JSON Output for Automation:
   lsblk -J

Outputs the device tree in JSON format, which can be parsed by other tools or scripts.

  1. Checking the UUID of a Partition:
   lsblk -o NAME,UUID

This command is useful for identifying partitions by their UUIDs.

Summary

  • Basic Command: lsblk provides a tree-like view of your block devices.
  • Custom Columns: Use -o to specify which columns to display.
  • Filesystem Info: -f shows filesystem types, labels, UUIDs, and mount points.
  • JSON/Pairs Format: Use -J or -P for script-friendly output.

The lsblk command is versatile and essential for managing and viewing storage devices in Linux, making it an invaluable tool for system administrators.