How to Use the lsblk Command to List Block Devices on Linux
lsblk is a command-line utility used for listing block devices on a Linux system. Block devices consist of storage devices that hold data in the form of blocks, which are, typically, hard disk drives (HDDs) or solid-state drives (SSDs).
The command displays information about block devices in a tree-like structure, with each device represented by a line in the output. The command gets its information from the sysfs file system. Note that lsblk doesn’t identify Random Access Memory (RAM) as a block device.
Installing lsblk on Linux
The lsblk command comes as part of the util-linux package. util-linux is a package of essential utilities for Linux systems that provides a wide range of functionality, including tools for managing files, disks, and system resources.
Some of the tools in the util-linux package include:
- fdisk: A utility for partitioning disks
- partx: Adds and removes partition definitions from the kernel
- swapon: Enables and disables swap devices and files
Your system would most probably have the util-linux package already installed, but in case it’s not, you can install it as follows depending on your Linux distro.
On Debian-based distros such as Ubuntu or MX Linux:
sudo apt-get install util-linux
On RHEL and its derivatives:
sudo yum install util-linux-ng
On Arch-based Linux distros such as Manjaro:
sudo pacman -S util-linux
Displaying Block Devices Using lsblk
To display all block devices on your system, simply run the command:
To display all devices including the empty ones, you can use the -a or –all option as follows:
Displaying Device Size in Bytes
By default, the lsblk command prints the unit size of block devices in a human-readable format. But if you want to feed the unit sizes to another program or script then you can use a different unit size that is machine or system friendly.
To display the unit size bytes without the unit symbol, run:
Displaying Device Owner and Group
In some cases, you might be interested in displaying the owner, group, and mode of the block devices. The full options for this command are -o NAME,SIZE,OWNER,GROUP,MODE, but this is just shortened to -m or –perms option.
Displaying Additional File System Information
To output information about your block devices’ file systems, you can use the -f option. Again, this is just a shorthand for the entire command options -o NAME,FSTYPE,FSVER,LABEL,UUID,FSAVAIL,FSUSE%,MOUNTPOINT.
The command outputs information such as the UUID (unique ID) of each device, device labels, mount points, file system types, etc.
Displaying Specific Columns in the Output
You can explicitly define the output columns that you want to list using the -o option. This is especially useful when you are creating custom scripts. Always define the columns that you want to output in your scripts because the default ones are bound to change.
To display only the size, name, and mount-point columns, you can use the command:
lsblk -o SIZE,NAME,MOUNTPOINT
You can include or exclude columns as required.
You can also choose to hide the header columns using the following command:
Formatting lsblk Output
The lsblk command also supports JSON formatted output, which is both human-readable and easy for machines to parse or generate. To display the data in JSON, you can run the command:
Getting Help for the lsblk Command
The lsblk command supports many options for customizing the block device output and filtering block devices. You can learn more about the command and its options from the man pages: man lsblk.
You can also use the help pages section to get help using the command:
Using Other Linux Commands From the util-linux Package
The lsblk command is a powerful tool for displaying information about block devices on your system. It comes as part of the util-linux package, which contains tools for managing and displaying disk info on Linux.
fdisk is one of the tools that come with util-linux, and you can use it for managing disk partitions on Linux.