How to Use the Command 'lsblk' (with examples)
- Linux
- December 17, 2024
The ’lsblk’ command in Linux is a powerful utility used to list information about all available or specified block devices. It produces a detailed, hierarchical view of storage devices, their partitions, sizes, and, optionally, their filesystem types and mount points. This tool is essential for systems administrators and developers who need to assess and manage disk resources on a Linux system efficiently. The command can display information in various formats and levels of detail, making it flexible for different use cases.
Use case 1: List all storage devices in a tree-like format
Code:
lsblk
Motivation:
The primary use of lsblk
without any options is to get a quick overview of all connected block storage devices in a system. This tree-like visualization is straightforward, making it easy to identify the device hierarchy, which includes disks and their respective partitions or logical volumes.
Explanation:
lsblk
: This base command lists all block devices connected to the system in a format that shows parent-child relationships.
Example output:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 931.5G 0 disk
├─sda1 8:1 0 512M 0 part /boot
└─sda2 8:2 0 931G 0 part /
sdb 8:16 0 223.6G 0 disk
└─sdb1 8:17 0 223.6G 0 part
Use case 2: Also list empty devices
Code:
lsblk -a
Motivation:
Sometimes, it’s important to see devices that may not be currently used or mounted. Listing all devices, including empty ones, helps identify unused or uninitialized disks that can be repurposed or configured.
Explanation:
-a
: This option ensures that all devices, including those not in use or empty, are displayed in the output. It’s vital when managing or auditing all hardware resources.
Example output:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 931.5G 0 disk
├─sda1 8:1 0 512M 0 part /boot
└─sda2 8:2 0 931G 0 part /
sdb 8:16 0 223.6G 0 disk
└─sdb1 8:17 0 223.6G 0 part
sdc 8:32 1 0B 1 disk
Use case 3: Print the SIZE column in bytes rather than in a human-readable format
Code:
lsblk -b
Motivation:
Displaying sizes in bytes can be crucial for certain technical scenarios, such as scripting, because it provides precision without any conversion involved. This accuracy helps in programmatically processing or comparing device sizes.
Explanation:
-b
: This option displays the sizes of devices in bytes, ensuring that there is no ambiguity or need for human-related size conversion.
Example output:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 1000204886016 0 disk
├─sda1 8:1 0 536870912 0 part /boot
└─sda2 8:2 0 999667488768 0 part /
sdb 8:16 0 240057409536 0 disk
└─sdb1 8:17 0 240057409536 0 part
Use case 4: Output info about filesystems
Code:
lsblk -f
Motivation:
Seeing the filesystem types assigned to each partition helps when determining the purpose of each disk or identifying unauthorized and erroneous configurations. This is fundamental for system administration and storage management.
Explanation:
-f
: Outputs the filesystem type for each block device, providing additional context for understanding the current use and configuration of disk storage.
Example output:
NAME FSTYPE LABEL UUID MOUNTPOINT
sda
├─sda1 ext4 1d2e471e-7c68-4d3e-9a55-1d4c37f9a611 /boot
└─sda2 ext4 3c965a7c-bd94-4cc9-bb29-2d4081c21131 /
sdb
└─sdb1 ext4 5d7c17b2-8d14-4f92-b3f9-0eaf20c8a42e
Use case 5: Use ASCII characters for tree formatting
Code:
lsblk -i
Motivation:
When working in environments where the display terminal may not support special or graphical characters, using ASCII can prevent rendering issues while still delivering readable and structured output.
Explanation:
-i
: This flag changes the tree formatting to use only ASCII characters, ensuring compatibility with legacy terminals or console-only access.
Example output:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 931.5G 0 disk
|--sda1 8:1 0 512M 0 part /boot
`--sda2 8:2 0 931G 0 part /
sdb 8:16 0 223.6G 0 disk
`--sdb1 8:17 0 223.6G 0 part
Use case 6: Output info about block-device topology
Code:
lsblk -t
Motivation:
Understanding the topology of block devices is useful for performance tuning and system architecture planning. It allows users to see the relationship between physical and logical volumes and how they are layered.
Explanation:
-t
: This flag outputs detailed information about the block-device topology, including the hierarchical structure and relationships between devices and partitions.
Example output:
NAME ALIGNMENT MIN-IO OPT-IO PHY-SEC LOG-SEC ROTA SCHED RQ-SIZE RA WSAME
sda 0 512 0 512 512 1 deadline 128 128 0B
├─sda1 0 512 0 512 512 1 deadline 128 128 0B
└─sda2 0 512 0 512 512 1 deadline 128 128 0B
sdb 0 512 0 512 512 1 deadline 128 128 0B
└─sdb1 0 512 0 512 512 1 deadline 128 128 0B
Use case 7: Exclude the devices specified by the comma-separated list of major device numbers
Code:
lsblk -e 1,7
Motivation:
In some situations, you might want to exclude certain devices from the output to focus on the devices you’re managing directly. This exclusion is useful for filtering out irrelevant devices like virtual file systems or loop devices.
Explanation:
-e 1,7
: This option excludes devices whose major device numbers are in the comma-separated list1,7
. It helps tailor the output, removing devices deemed unnecessary for the current task.
Example output:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 931.5G 0 disk
├─sda1 8:1 0 512M 0 part /boot
└─sda2 8:2 0 931G 0 part /
sdb 8:16 0 223.6G 0 disk
└─sdb1 8:17 0 223.6G 0 part
Use case 8: Display a customized summary using a comma-separated list of columns
Code:
lsblk --output NAME,SERIAL,MODEL,TRAN,TYPE,SIZE,FSTYPE,MOUNTPOINT
Motivation:
Customizing the columns to display allows you to focus on specific characteristics of devices that are pertinent to your task. Whether it’s for documentation or troubleshooting, having just the relevant details avoids clutter.
Explanation:
--output NAME,SERIAL,MODEL,TRAN,TYPE,SIZE,FSTYPE,MOUNTPOINT
: This option specifies a custom list of columns to be shown, providing only the essential information required for the current context.
Example output:
NAME SERIAL MODEL TRAN TYPE SIZE FSTYPE MOUNTPOINT
sda S1Z8J9AH304349T MZ7LN512HMTP-000L1 sata disk 500G
├─sda1 partition 500M ext4 /boot
└─sda2 partition 499G ext4 /
sdb WD-WX61EA5RJ5C3 WDC WD10EZEX sata disk 1T
└─sdb1 partition 1T ntfs
Conclusion:
The lsblk
command offers a variety of options to list block devices and their properties in Linux, accommodating different needs from basic listings to complex use cases involving filtering and formatting. By understanding and utilizing these options, users can effectively manage their system’s disk resources, ensuring that they maintain control and can extract the necessary information with precision and ease.