lsblk Command in Linux

Learn via video courses
Topics Covered

Overview

Welcome to the world of Linux commands where we take a deep dive into the lsblk command in Linux. The 'lsblk' stands for 'list block devices', and as the name suggests, it is used to list out all block devices in a tree-like format. This powerful command can help you gather comprehensive information about each block device connected to your Linux system, including the disk partitions and their respective sizes.

Syntax of lsblk Command in Linux

The basic syntax of the lsblk command in Linux is as follows:

Where:

  • options: These are the flags or parameters that can be used with the lsblk command to modify its output or behavior.
  • device: This refers to the specific block device(s) that you want to display information for. If no device is specified, lsblk will display information for all block devices.

Options in lsblk Command in Linux

  1. -a, --all: Shows all block devices, including the empty ones.

    For example -

    Output:

    This command lists out all block devices, even if they don't have any filesystems mounted on them.

  2. -b, --bytes: Prints the size in bytes instead of in a human-readable format.

    For example -

    Output:

    This command displays the size of the block devices in bytes, which can be useful when precise measurements are required.

  3. -o, --output [list]: Allows you to customize the output by specifying a comma-separated list of column names.

    For example -

    Output:

    This command customizes the output of the lsblk command to only display the name and size of each block device.

Example Usages

  • Basic usage of lsblk command:

    Output:

    Explanation: Without any options, the lsblk command in Linux lists out all block devices, along with relevant information such as their size, type, and mountpoint.

  • Using lsblk command to display specific devices:

    Output:

    Explanation: By specifying a device in the command, lsblk only displays information about that particular device.

Tips

  • The lsblk command in Linux is a great way to get an overview of your system's storage devices. However, for detailed disk usage statistics, consider using the 'df' command.

  • Remember to run the 'lsblk' command with superuser privileges (using sudo) if you're not seeing all the information you expect.

Advanced Use Cases of lsblk Command in Linux

  • Using lsblk to display the UUID and file system type of devices:

    Output:

    Explanation: This advanced usage of the lsblk command in Linux allows you to display the UUID and file system type of block devices, which can be useful for troubleshooting and system configuration.

  • Using lsblk to display the read-only block devices:

    Output:

    Explanation: This advanced usage of the lsblk command helps you to identify the read-only block devices in your system.

  • Using lsblk to exclude certain types of devices:

    Output:

    Explanation: The '-e' option allows you to exclude devices by their major number, providing a more focused output.

Conclusion

  • The lsblk command in Linux provides a comprehensive way to view information about all block devices in a system.

  • It offers a range of options to tailor the output according to your needs.

  • Advanced usages include displaying the UUID and file system type of devices, identifying read-only devices, and excluding certain types of devices.