Master the Linux ‘lsblk’ Command: A Comprehensive Guide

Peter Hou
3 min readJun 9, 2023

--

In this article, we delved into the Linux lsblk command. The guide explored the command's history, its uses, and various options, along with practical applications and potential issues. Through mastering the lsblk command, one can effectively manage and explore block devices in a Linux environment.

Instructions

This article provides a comprehensive guide to the Linux lsblk command. We will explore the command's history, the rationale for its creation, its detailed usage, options, and practical applications. The guide also includes potential issues and best practices related to the lsblk command.

History

The lsblk command, short for "list block devices", is part of the util-linux package which has been distributed with Linux since the early days of the operating system.

When and why to use it

lsblk is primarily used when you need to list all block devices, along with their sizes and types, on a Linux system. This becomes particularly useful when you need to mount a new device or debug storage issues.

How to use it

The basic usage of lsblk is straightforward. Running the command without any options will list all block devices in a tree-like format.

$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 238.5G 0 disk
├─sda1 8:1 0 512M 0 part /boot/efi
└─sda2 8:2 0 238G 0 part /

The commonly used parameters

  • -l presents the output in a list format, rather than a tree.
$ lsblk -l
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 238.5G 0 disk
sda1 8:1 0 512M 0 part /boot/efi
sda2 8:2 0 238G 0 part /
  • -f includes filesystem information in the output.
$ lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
sda
├─sda1 vfat 1234-5678 /boot/efi
└─sda2 ext4 12345678-1234-1234-1234-123456789012 /

Other supported parameters

  • -b displays the size of the blocks in bytes.
  • -m provides a more human-readable format, displaying sizes in powers of 1024.
  • -a shows all block devices, including empty ones.
  • -p includes the full path of each device in the output.
  • -r produces raw output, suitable for use in scripts.

Most common use cases

One of the most common use cases of lsblk is identifying a newly connected device's block ID to mount it.

$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 238.5G 0 disk
├─sda1 8:1 0 512M 0 part /boot/efi
└─sda2 8:2 0 238G 0 part /
sdb 8:16 1 14.9G 0 disk

The tricky skills

A nifty trick is using lsblk with the -J option to produce JSON formatted output, which can be handy for scripting.

$ lsblk -J
{
"blockdevices": [
{"name": "sda", "maj:min": "8:0", "rm": "0", "size": "238.5G", "ro": "0", "type": "disk", "mountpoint": null,
"children": [
{"name": "sda1", "maj:min": "8:1", "rm": "0", "size": "512M", "ro": "0", "type": "part", "mountpoint": "/boot/efi"},
{"name": "sda2", "maj:min": "8:2", "rm": "0", "size": "238G", "ro": "0", "type": "part", "mountpoint": "/"}
]
}
]
}

What needs to be noted

Be aware that the lsblk command only shows block devices. If you are interested in other types of devices, such as character devices or network interfaces, you will need to use other tools.

Conclusion

The lsblk command is a powerful tool for managing and exploring block devices on a Linux system. By understanding and making use of the options and output formats available, it can be instrumental in maintaining and troubleshooting your Linux environment.

--

--

Peter Hou
Peter Hou

Written by Peter Hou

I am a Senior Software Engineer and tech lead in a top tech company.

No responses yet