Change lsblk
sort order
lsblk
sorts partitions in an order that is hard to follow. A method of sorting it without having to write a bash script or python script would be the first choice.
A similar question was asked in Unix & Linux:
However the answer there to use -x NAME
parameter for sorting causes the tree indenting to disappear.
Current sort order
This is how lsblk
displays now:
$ lsdrv
NAME FSTYPE LABEL MOUNTPOINT SIZE MODEL
nvme0n1 477G Samsung SSD 960 PRO 512GB
├─nvme0n1p9 swap [SWAP] 7.9G
├─nvme0n1p7 ext4 Old_Ubuntu_16.04 /mnt/old 23.1G
├─nvme0n1p5 ntfs 859M
├─nvme0n1p3 16M
├─nvme0n1p1 ntfs 450M
├─nvme0n1p8 ntfs Shared_WSL+Linux /mnt/e 9G
├─nvme0n1p10 ext4 Ubuntu_18.04 /mnt/clone 27.2G
├─nvme0n1p6 ext4 New_Ubuntu_16.04 / 45.1G
├─nvme0n1p4 ntfs NVMe_Win10 /mnt/c 363.2G
└─nvme0n1p2 vfat /boot/efi 99M
mmcblk0 119.1G
└─mmcblk0p1 vfat SANDISK128 /media/rick/SANDISK128 119.1G
sr0 1024M DVD+/-RW DW316
sda 931.5G HGST HTS721010A9
├─sda4 ntfs WINRETOOLS 450M
├─sda2 128M
├─sda5 ntfs Image 11.4G
├─sda3 ntfs HGST_Win10 /mnt/d 919G
└─sda1 vfat ESP 500M
Proposed sort order
This is how it lsblk
should be sorted:
$ lsdrv
NAME FSTYPE LABEL MOUNTPOINT SIZE MODEL
nvme0n1 477G Samsung SSD 960 PRO 512GB
├─nvme0n1p1 ntfs 450M
├─nvme0n1p2 vfat /boot/efi 99M
├─nvme0n1p3 16M
├─nvme0n1p4 ntfs NVMe_Win10 /mnt/c 363.2G
├─nvme0n1p5 ntfs 859M
├─nvme0n1p6 ext4 New_Ubuntu_16.04 / 45.1G
├─nvme0n1p7 ext4 Old_Ubuntu_16.04 /mnt/old 23.1G
├─nvme0n1p8 ntfs Shared_WSL+Linux /mnt/e 9G
├─nvme0n1p9 swap [SWAP] 7.9G
└─nvme0n1p10 ext4 Ubuntu_18.04 /mnt/clone 27.2G
mmcblk0 119.1G
└─mmcblk0p1 vfat SANDISK128 /media/rick/SANDISK128 119.1G
sr0 1024M DVD+/-RW DW316
sda 931.5G HGST HTS721010A9
├─sda1 vfat ESP 500M
├─sda2 128M
├─sda3 ntfs HGST_Win10 /mnt/d 919G
├─sda4 ntfs WINRETOOLS 450M
└─sda5 ntfs Image 11.4G
Note: lsdrv
is an alias defined in ~/.bashrc
:
$ alias lsdrv
alias lsdrv='lsblk -o NAME,FSTYPE,LABEL,MOUNTPOINT,SIZE,MODEL |egrep -v "^loop"'
Summary
Complexities of changing sort order are:
- Only partitions with line draw characters
├─
and └─
are sorted under their drives.
- After sorting partitions, the last partition may need
├─
replaced with └─
.
- After sorting partitions, the first partition to second last partition may need
└─
replaced with ├─
.
- The old "Achilles' heel" of sorting places
10
after 1
, E.G. 1
then 10
then 2
. Really 9
should appear before 10
.
The sort order of lsblk
has been a thorn for years. Hopefully someone has a simple solution with GNU utiliies like: awk
, sed
, grep
, uniq
and/or sort
, etc.