Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Use the \'lsblk\' Command to List Block Devices in Linux

The lsblk command is an essential utility in Linux for listing information about all available or specified block devices. It provides a tree-like representation of the storage devices, making it easier to understand the relationship between different storage components. This command is particularly useful for system administrators and engineers who need to manage disk partitions and storage configurations.

Understanding the lsblk Command

The lsblk command displays information about block devices, which are storage devices that support random access to fixed-size blocks of data. These include hard drives, SSDs, CD-ROMs, and other storage devices. The command outputs details such as device names, major and minor device numbers, sizes, types, and mount points.

Basic Usage

The simplest form of the lsblk command is executed without any arguments:

lsblk

This command will list all block devices in a tree format, showing their hierarchy and mount points.

Displaying All Devices

To include empty devices in the list, use the -a option:

lsblk -a

Displaying Detailed Information

For more detailed information about each block device, use the -o option to specify the columns you want to display. For example, to display the device name, size, type, and mount point, use:

lsblk -o NAME,SIZE,TYPE,MOUNTPOINT

Displaying Information in JSON Format

To get the output in JSON format, which is useful for scripting and automation, use the -J option:

lsblk -J

Filtering by Device Type

If you want to list only specific types of devices, such as disks or partitions, use the -t option. For example, to list only disk devices:

lsblk -d

Examples

  1. List All Block Devices with Detailed Information:

    lsblk -o NAME,MAJ:MIN,RM,SIZE,RO,TYPE,MOUNTPOINT

    This command will provide a comprehensive view of all block devices, including removable status (RM), read-only status (RO), and their mount points.

  2. List All Partitions:

    lsblk -l -o NAME,SIZE,TYPE | grep part

    This command lists all partitions by filtering the output for lines containing "part".

  3. Display Block Devices in a Script-Friendly Format:

    lsblk -P

    The -P option outputs the information in key-value pairs, making it easier to parse in scripts.

Conclusion

The lsblk command is a powerful tool for managing and understanding the block devices on a Linux system. Whether you're setting up new storage, troubleshooting existing configurations, or simply exploring the system, lsblk provides the necessary insights into your storage devices.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.