The Wayback Machine - https://web.archive.org/web/20241228134502/https://www.geeksforgeeks.org/lsblk-command-in-linux-with-examples/
Open In App

How to List All Block Devices in Linux | lsblk Command

Last Updated : 15 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Understanding the storage devices connected to your Linux system is crucial for efficient system management. The ‘lsblk' command, short for “list block devices,” is a powerful tool that provides detailed information about block devices such as hard drives, solid-state drives, and other storage-related devices.

Here, we will look into the ‘lsblk' command, exploring its syntax, options, and practical implementations.

What is the ‘lsblk’ Command?

‘lsblk’ is used to display details about block devices and these block devices(Except ram disk) are basically those files that represent devices connected to the pc. It queries ‘/sys’ virtual file system and ‘udev db’ to obtain information that it displays. And it basically displays output in a tree-like structure. This command is part of the util-Linux package, which comes pre-installed on many Linux distributions, providing users with essential system management utilities.

Installing lsblk Command in Linux

Many Linux distributions do not have lsblk command pre-installed. To install it use the following commands as per your Linux distribution. 

1. In case of Debian/Ubuntu  

sudo apt-get install util-linux

2. In case of CentOS/RedHat  

sudo yum install util-linux-ng

3. In case of Fedora OS  

sudo yum install util-linux-ng

Syntax of lsblk Command in Linux

The basic syntax of the lsblk command is simple:

lsblk [OPTIONS] [DEVICE...]

Here,

  • [OPTIONS] represents the various flags and parameters that can be used with the command.
  • [DEVICE...] refers to the specific block devices you want to list. If no device is specified, lsblk will display information for all available block devices.

Options Available in lsblk Command

Options

Description

-a or --all

Display all devices, including empty ones.

-b or --bytes

Print sizes in bytes instead of the more human-readable formats.

-i or --inverse

Invert the output, showing only non-empty devices.

-l or --list

Print output in a list format.

-o or --output

Define the output columns to display.

-p or --pairs

Display key-value pairs for each device.

-t or --tree

Print devices in a tree-like format.

-x or --exclud

Exclude devices that match the specified criteria.

Practical Example of lsblk Command in Linux

1. How to List All Block Devices in Linux

To display block devices. 

lsblk 

This command will display a hierarchical view of all block devices connected to your system, including information about partitions and mount points.

to-display-block-devices

2. How to Display All Devices, Including Empty Ones

To display empty block devices as well.  

lsblk -a 

This command includes all devices, even empty ones, in the output. It can be useful when you want to see all available devices, including those without any partitions or mount points.

to-display-empty-block-devices

3. How to Print sizes in bytes instead of the more human-readable formats

To print size information in bytes. 

lsblk -b

The ‘-b' option displays sizes in bytes. This can be handy when you need precise size information for block devices.

display-block-devices-in-bytes

4. How to Viewing Devices in a Tree-Like Format

The ‘-t' option organizes devices in a tree-like format, making it easier to visualize the relationships between devices and partitions.

lsblk -t
viewing devices in tree-like format

viewing devices in tree-like format

5. To print zone model for devices.  

lsblk -z 

print-zone-model-block-devices

6. To skip slave entries

lsblk -d 

This will skip all the slave entries. 

to-avoid-slaves

7. How to Invert the output, showing only non-empty devices

To use ASCII characters for tree formatting.  

lsblk -i 

Inverting the output with ‘-i' will show only non-empty devices. This is useful when you want to focus on devices with existing partitions or mount points.

block-devices-ASCII-tree-formatting

8. To print information about device owner, group, and mode of block devices.  

This will display all details of device owner, group and mode of block devices. 

lsblk -m 

to-display-mode-owner-group-block-device

9. How to Define the output columns to display

To print selected columns of block-devices. 

lsblk -o SIZE, NAME, MOUNTPOINT 

Use ‘-o' to customize the output columns. In this example, we specify that we only want to see the device name, size, and mount point for each device.

to-display-specific-columns

This will print only the specified columns. 

10. To hide column headings

This will hide column headings. 

lsblk -dn

hide-column-heading-block-devices

11. To display help section of the command

lsblk --help 

This will display the help section of the command.

Conclusion

In this article we discussed the ‘lsblk’ command which is a vital tool for managing storage devices in Linux, offering detailed insights into hard drives and SSDs. This article covers its syntax, options, and practical applications. ‘lsblk’, part of the util-Linux package, efficiently queries system files to present a tree-like view of block devices. Examples demonstrate how to list devices, customize output, and visualize storage efficiently. Mastering ‘lsblk’ enhances system administration, allowing users to navigate and manage storage resources effectively.

How to List All Block Devices in Linux – FAQs

What is the lsblk command used for in Linux?

The ‘lsblk' (list block devices) command in Linux is used to display information about all available or the specified block devices. It lists information such as device name, mount point, size, and type, among other details. ‘lsblk' reads the sysfs filesystem and udev db to gather information, making it a reliable and fast tool for getting details about block devices without the need to run it with superuser privileges.

How to display all block devices in Linux?

To display all block devices on your system along with their associated partitions and storage hierarchy, simply run the lsblk command without any additional options:

lsblk

This command will list all block devices such as hard drives, partitions, and removable media, along with details like size, type, and relationship (e.g., partitions within drives).

Can lsblk show the mount point of devices?

Yes, ‘lsblk' can show the mount points of devices. By default, the output of ‘lsblk' includes the MOUNTPOINT column which shows where the device is mounted in the filesystem. If the column does not appear, you can specifically include it using the ‘-o' option:

lsblk -o NAME,FSTYPE,SIZE,MOUNTPOINT

This command lists devices with their names, filesystem types, sizes, and mount points.

How to use lsblk to show the size of devices?

'lsblk' by default shows the size of the devices. If you want to customize the display to show specific columns, including the size, you can use the ‘-o' option to specify which columns to display. For example:

lsblk -o NAME,SIZE,TYPE,MOUNTPOINT

This command will show each device’s name, size, type (like disk or partition), and where it’s mounted.

What are some common options for the lsblk command?

'lsblk' provides several options that can enhance the output or refine what information is displayed:

  • -f, –fs: Shows the filesystem type in addition to the default output.
  • -l, –list: Displays the information in a list format instead of a tree view, which can be useful for parsing in scripts.
  • -m, –perms: Displays the permissions and owner of each device.
  • -o, –output LIST: Allows you to specify a comma-separated list of columns you want to display. For example, NAME,SIZE,FSTYPE,LABEL,UUID,MOUNTPOINT.
  • -d, –no-deps: Only lists devices, without their partitions or slaves, which simplifies the output if you’re only interested in disk-level information.
  • -i, –ascii: Use ASCII characters for tree formatting, useful for environments that do not support Unicode.


    Next Article

    Similar Reads

    three90RightbarBannerImg