Listing disks and partitions with lsblk
- Version
- unversioned
- Review
- Draft
Summary#
lsblk lists block devices such as disks, partitions and logical volumes as a tree, with sizes and mount points.
Syntax#
lsblk
lsblk -f
lsblk -o NAME,SIZE,TYPE,MOUNTPOINTS
lsblk -J -b
sudo blkid /dev/sda1
-f adds filesystem types, labels and UUIDs. -o chooses columns, -J prints JSON and -b prints sizes in bytes. blkid reads identifiers from a device and may need root.
Example#
(
set -eu
lsblk -d -o NAME,SIZE,TYPE | awk 'NR == 1 { print $1, $2, $3 }'
lsblk -J -b -d -o NAME,SIZE | grep -c '"blockdevices"'
)
Expected output#
Device names and sizes differ between machines, so the script prints the column header and checks the JSON key.
NAME SIZE TYPE
1
Common mistakes#
Check the size, model and mount points before writing to a device: names such as /dev/sda can change between boots. Refer to filesystems by UUID or label in /etc/fstab. A disk without a mount point is not necessarily empty; it can hold a RAID member, a swap area, an LVM volume or unmounted data. lsblk does not report free space inside a filesystem; use df for that.
Version applicability#
This entry describes lsblk from util-linux 2.41. The MOUNTPOINTS column appeared in util-linux 2.37; older releases have MOUNTPOINT.
Sources#
- Attribution
- DevLookup contributors
- License
- CC-BY-4.0