1

I need to get the output of df -h sorted alphabetically by its mount point in the Mounted on column.

However, df -h currently outputs similar to this:

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/nvme0n1p2  228G   14G  203G   7% /
/dev/nvme0n1p1  511M  7.9M  504M   2% /boot/efi
/dev/sdf2        17T   17T  3.3T  56% /mnt/data/drive014
/dev/sdl1        17T   17T   53G 100% /mnt/data/drive006
/dev/sdd1        17T   17T  102G 100% /mnt/data/drive002
/dev/sda1        17T   17T   26G 100% /mnt/data/drive001

Is there a command to get it output like below?

Filesystem      Size  Used Avail Use% Mounted on
/dev/nvme0n1p2  228G   14G  203G   7% /
/dev/nvme0n1p1  511M  7.9M  504M   2% /boot/efi
/dev/sda1        17T   17T   26G 100% /mnt/data/drive001
/dev/sdd1        17T   17T  102G 100% /mnt/data/drive002
/dev/sdl1        17T   17T   53G 100% /mnt/data/drive006
/dev/sdf2        17T   17T  3.3T  56% /mnt/data/drive014
Nyxynyx
  • 1,459
  • 11
  • 39
  • 49

1 Answers1

4

With bash, you can do this:

df -h | { read -r line; echo "$line"; sort -k 6,6; }

That reads (consumes) and prints the first (header) line, then sort will consume all the rest.

glenn jackman
  • 4,630
  • 1
  • 17
  • 20