Linux – Does lsblk always list disks in sorted ascending order

filesystemslinuxrhel

Does lsblk always list disks in ascending order (each device letter increasing by one ) and sorted?

For example:

lsblk | awk '{print $1}'
NAME
fd0
sda
├─sda1
└─sda2
├─vg00-lv_root
├─vg00-lv_swap
└─vg00-lv_var
sdb
sdc
sdd
sde
sdf
sdg
sdh
sdi
sdj
sdk

We do not want to get it like the examples below

Example of non ordered disk list (output edited for the example )

lsblk | awk '{print $1}'
NAME
fd0
sda
├─sda1
└─sda2
├─vg00-lv_root
├─vg00-lv_swap
└─vg00-lv_var
sdb
sdc
sdd
sde
sdi
sdj
sdk
sdf
sdg
sdh

Example of disks starting with sdd instead of sdb (again, edited for the example )

lsblk | awk '{print $1}'
NAME
fd0
sda
├─sda1
└─sda2
├─vg00-lv_root
├─vg00-lv_swap
└─vg00-lv_var
sdd
sde
sdf
sdg
sdh
sdi
sdj
sdk
sdl
sdm

Best Answer

From the lsblk(1) manual page:

The default output, as well as the default output from options like --fs and --topology, is subject to change. So whenever possible, you should avoid using default outputs in your scripts. Always explicitly define expected columns by using --output columns-list in environments where a stable output is required.

If you require the output to be sorted, I'd suggest using the -o (output columns) and -x (sort column) options. Use lsblk --help to get a list of the column names.

For example:

lsblk -o NAME -x NAME
Related Question