How to use the command 'lsblk' (with examples)

How to use the command 'lsblk' (with examples)

The ’lsblk’ command is used to list information about storage devices such as hard drives, SSDs, and USB drives. It provides a tree-like format to display the devices and their properties. This command is especially useful for system administrators or users who need to gather information about the storage devices connected to their system.

Use case 1: List all storage devices in a tree-like format

Code:

lsblk

Motivation: This use case is helpful when you want to view all the storage devices connected to your system in a structured and organized way.

Explanation: The ’lsblk’ command without any options will display all storage devices in a tree-like format. It shows the devices as a hierarchy, with their names, sizes, and mount points. By default, only the devices that contain a filesystem and are visible to the user are shown.

Example output:

NAME    SIZE        TYPE  MOUNTPOINT 
sda     500G        disk  
├─sda1  255M        part  /boot/efi
├─sda2  1K          part  
├─sda3  496.3G      part  /
└─sda4  3.7G        part  [SWAP]

Use case 2: Also list empty devices

Code:

lsblk -a

Motivation: Including empty devices in the output is useful when you want to see all the storage devices, even if they don’t contain a filesystem or are not currently mounted.

Explanation: Adding the ‘-a’ option to the ’lsblk’ command will also list empty devices in the output. Empty devices are shown with a ‘-’ symbol under the ‘MOUNTPOINT’ column.

Example output:

NAME    SIZE        TYPE  MOUNTPOINT 
sda     500G        disk  
├─sda1  255M        part  /boot/efi
├─sda2  1K          part  
├─sda3  496.3G      part  /
└─sda4  3.7G        part  [SWAP]
sdb     1T          disk  
sdc     2T          disk  -

Use case 3: Print the SIZE column in bytes

Code:

lsblk -b

Motivation: When you need the actual size of the storage devices in bytes instead of the human-readable format, this use case comes in handy.

Explanation: The ‘-b’ option modifies the ‘SIZE’ column to display the size of the devices in bytes. By default, the ‘SIZE’ column is shown in a human-readable format, such as ‘500G’ for 500 gigabytes.

Example output:

NAME    SIZE           TYPE  MOUNTPOINT 
sda     500107862016  disk  
├─sda1  262144000      part  /boot/efi
├─sda2  4096           part  
├─sda3  532676442112   part  /
└─sda4  4076863488     part  [SWAP]

Use case 4: Output info about filesystems

Code:

lsblk -f

Motivation: If you want to gather information solely about the filesystems on the devices, this use case proves helpful.

Explanation: The ‘-f’ option displays additional information about the filesystems on the storage devices. It shows the filesystem type under the ‘FSTYPE’ column.

Example output:

NAME    SIZE        TYPE  FSTYPE  MOUNTPOINT 
sda     500G        disk          
├─sda1  255M        part  vfat    /boot/efi
├─sda2  1K          part          
├─sda3  496.3G      part  ext4    /
└─sda4  3.7G        part  swap    [SWAP]

Use case 5: Use ASCII characters for tree formatting

Code:

lsblk -i

Motivation: ASCII tree formatting provides a simpler and more compact visualization of the storage devices.

Explanation: Adding the ‘-i’ option to the ’lsblk’ command will switch the tree formatting to ASCII characters. This format uses ASCII characters such as ‘|’ and ‘-’ to represent the hierarchy of devices.

Example output:

NAME    SIZE        TYPE  MOUNTPOINT 
sda     500G        disk  
|-sda1  255M        part  /boot/efi
|-sda2  1K          part  
|-sda3  496.3G      part  /
`-sda4  3.7G        part  [SWAP]

Use case 6: Output info about block-device topology

Code:

lsblk -t

Motivation: When you need detailed information about the block-device topology and relationships, this use case is useful.

Explanation: The ‘-t’ option provides information about the block-device topology. It shows the devices with their hierarchy, including parent, children, and siblings.

Example output:

NAME    SIZE        TYPE  MOUNTPOINT 
sda     500G        disk          
`-sda1  255M        part  /boot/efi
`-sda2  1K          part          
`-sda3  496.3G      part  /
`-sda4  3.7G        part  [SWAP]

Use case 7: Exclude specified devices

Code:

lsblk -e 1,7

Motivation: If you want to exclude specific devices from the output, this use case allows you to specify major device numbers to exclude.

Explanation: The ‘-e’ option is followed by a comma-separated list of major device numbers. Devices with these major device numbers are excluded from the ’lsblk’ output.

Example output:

NAME    SIZE        TYPE  MOUNTPOINT 
sda     500G        disk  
├─sda1  255M        part  /boot/efi
├─sda2  1K          part  
└─sda3  496.3G      part  /

Use case 8: Display a customized summary using specific columns

Code:

lsblk --output NAME,SERIAL,MODEL,TRAN,TYPE,SIZE,FSTYPE,MOUNTPOINT

Motivation: When you want to customize the output and display only specific columns, this use case allows you to choose the columns you are interested in.

Explanation: The ‘–output’ option is followed by a comma-separated list of column names. Only the specified columns will be displayed in the output.

Example output:

NAME    SERIAL     MODEL           TRAN   TYPE  SIZE        FSTYPE  MOUNTPOINT 
sda                Samsung_SSD_850  sata   disk  500G        ext4    /          
├─sda1             Samsung_SSD_850  sata   part  255M        vfat    /boot/efi  
├─sda2             Samsung_SSD_850  sata   part  1K                  
└─sda3             Samsung_SSD_850  sata   part  496.3G      ext4              

Conclusion:

The ’lsblk’ command is a versatile tool for listing information about storage devices. It provides various options to customize the output, including tree-like formatting, displaying empty devices, modifying the size display format, and outputting additional information about filesystems and block-device topology. By using the appropriate options, users can efficiently gather the necessary information about their storage devices.

Related Posts

How to use the command gitwatch (with examples)

How to use the command gitwatch (with examples)

Gitwatch is a command that allows you to automatically commit file or directory changes to a git repository.

Read More
Using flyctl Command (with examples)

Using flyctl Command (with examples)

1: Sign into a Fly account flyctl auth login Motivation: The auth login command is used to authenticate and sign into a Fly account.

Read More
Using pngquant to Compress PNG Images (with examples)

Using pngquant to Compress PNG Images (with examples)

Code Examples Compress a specific PNG as much as possible and write result to a new file: pngquant path/to/file.

Read More