Linux Command Reference ยท Disks and Filesystems
Linux lsblk command
Reference for the Linux lsblk command: list disks, partitions and their mount points as a tree, choose columns with -o, get script-friendly output with -P and -J.
lsblk lists block devices (disks, partitions, LVM volumes, loop devices) in a tree that shows how they nest and where they are mounted. It reads the kernel's view from /sys and udev, needs no root rights, and does not touch the devices themselves, so it is the safe first command when you plug in a disk and want to know what it is called.
It is the friendly alternative to reading /proc/partitions or running fdisk -l.
lsblk [OPTION]... [DEVICE]...
Try it
The sandbox has no /sys/dev/block, so lsblk itself prints an error here. Both examples therefore work on sample output that you would get from a real machine; the parsing code is the part you can reuse.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
lsblk [-a] [-b] [-d] [-f] [-l] [-n] [-p] [-r] [-J | -P] [-o COLUMNS] [-t] [DEVICE]...
| Option | Description |
|---|---|
-a, --all |
Also list empty devices (such as a card reader without a card). |
-b, --bytes |
Print sizes in bytes instead of 476.9G. |
-d, --nodeps |
Show only the devices themselves, not their partitions. |
-f, --fs |
Show filesystem information: NAME,FSTYPE,LABEL,UUID,MOUNTPOINT. |
-l, --list |
List format instead of a tree. |
-n, --noheadings |
Do not print the header line. |
-p, --paths |
Full device paths (/dev/sda1). |
-r, --raw |
Raw output with escaped spaces, good for awk. |
-J, --json |
JSON output. |
-P, --pairs |
KEY="value" pairs, one device per line. |
-o COLUMNS |
Choose columns, for example NAME,SIZE,TYPE,FSTYPE,MOUNTPOINT. lsblk --help lists all of them. |
-t, --topology |
I/O topology (alignment, scheduler, request sizes). |
-e CODES |
Exclude devices by major number. -e7 hides loop devices. |
Useful column names: NAME, SIZE, TYPE (disk, part, lvm, loop, rom, crypt), RO, RM (removable), MOUNTPOINT, FSTYPE, LABEL, UUID, MODEL.
Returns: 0 on success; 1 on failure; 32 if a requested device was not found; 64 if only some devices were found. It fails if /sys is unavailable, as in minimal containers.
Usage notes
- No root needed for the basic listing. Reading filesystem types, labels and UUIDs (
-f) uses theudevdatabase, or may need root when that database is empty.blkidprobes the devices directly. - Before you format or write to a device, check the
NAME,SIZE,MODELandMOUNTPOINTcolumns. Mixing up/dev/sdaand/dev/sdbwithddormkfsdestroys data. - Names change.
sdaandsdbcan swap between boots. Identify disks byUUID,LABELor/dev/disk/by-id/in configuration files, not bysdX. - Mount points are for mounted filesystems only. A partition with an empty
MOUNTPOINTmay still be in use as swap ([SWAP]) or by LVM. - Parsing. Prefer
-Por-J(withjq) over cutting columns: names may contain spaces in labels, and the tree characters (โโ) are in the first column unless you pass-lor-r. - Loop devices for snap packages and disk images clutter the list on some systems; hide them with
-e7. - Containers usually lack
/sys/dev/block, solsblkprints "failed to access sysfs directory" and exits non-zero. Usefindmntordfthere. lsblkshows structure, not usage. For free space usedf; for partition tables usefdisk and parted.
Related
blkid,findmnt,mount,fdisk and parted,df- Lessons: Disks and Logs, LVM and Filesystems