Filtering Block Devices with lsblk
In this step, we will learn how to filter the output of the lsblk
command to focus on specific types of block devices or display information in different formats.
Filtering by Device Type
The lsblk
command allows you to filter devices by their type using the --type
or -t
option. Common device types include:
disk
: Physical disks
part
: Partitions
loop
: Loop devices
lvm
: Logical volumes
To display only disk devices, run the following command:
lsblk --type disk
The output will show only the main disks, without their partitions:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 50G 0 disk
Similarly, to display only partition devices, run:
lsblk --type part
The output will show only the partitions:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda1 8:1 0 49G 0 part /
sda2 8:2 0 976M 0 part [SWAP]
Displaying Device Paths
The --paths
option displays the full device paths rather than just the device names. This is useful when you need to reference the devices in scripts or commands.
Run the following command:
lsblk --paths
The output will include the full device paths:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
/dev/loop0 7:0 0 55.5M 1 loop /snap/core18/2128
/dev/loop1 7:1 0 55.4M 1 loop /snap/core18/2284
/dev/loop2 7:2 0 43.6M 1 loop /snap/snapd/15534
/dev/loop3 7:3 0 61.9M 1 loop /snap/gtk-common-themes/1535
/dev/loop4 7:4 0 31.1M 1 loop /snap/snapd/16292
/dev/sda 8:0 0 50G 0 disk
├─/dev/sda1 8:1 0 49G 0 part /
└─/dev/sda2 8:2 0 976M 0 part [SWAP]
The --json
option outputs the information in JSON format, which is useful for programmatic processing or when integrating with other tools.
Run the following command:
lsblk --json
The output will be in JSON format:
{
"blockdevices": [
{
"name": "loop0",
"maj:min": "7:0",
"rm": false,
"size": "55.5M",
"ro": true,
"type": "loop",
"mountpoint": "/snap/core18/2128"
},
{
"name": "loop1",
"maj:min": "7:1",
"rm": false,
"size": "55.4M",
"ro": true,
"type": "loop",
"mountpoint": "/snap/core18/2284"
},
{
"name": "loop2",
"maj:min": "7:2",
"rm": false,
"size": "43.6M",
"ro": true,
"type": "loop",
"mountpoint": "/snap/snapd/15534"
},
{
"name": "loop3",
"maj:min": "7:3",
"rm": false,
"size": "61.9M",
"ro": true,
"type": "loop",
"mountpoint": "/snap/gtk-common-themes/1535"
},
{
"name": "loop4",
"maj:min": "7:4",
"rm": false,
"size": "31.1M",
"ro": true,
"type": "loop",
"mountpoint": "/snap/snapd/16292"
},
{
"name": "sda",
"maj:min": "8:0",
"rm": false,
"size": "50G",
"ro": false,
"type": "disk",
"children": [
{
"name": "sda1",
"maj:min": "8:1",
"rm": false,
"size": "49G",
"ro": false,
"type": "part",
"mountpoint": "/"
},
{
"name": "sda2",
"maj:min": "8:2",
"rm": false,
"size": "976M",
"ro": false,
"type": "part",
"mountpoint": "[SWAP]"
}
]
}
]
}
Combining Options for Precise Control
You can combine multiple options to get exactly the information you need. For example, to display only disk devices with their full paths in JSON format:
lsblk --type disk --paths --json
The output will be a JSON representation of only the disk devices with their full paths:
{
"blockdevices": [
{
"name": "/dev/sda",
"maj:min": "8:0",
"rm": false,
"size": "50G",
"ro": false,
"type": "disk"
}
]
}
These filtering options make the lsblk
command very flexible and allow you to get precisely the information you need about block devices on your system.