6

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,509

3 Answers3

8

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.

1

There is bash native way to do it with the next command:

LC_ALL=C df -lk --output=target,source,fstype,size,used,avail,pcent | (sed -u 1q; sort -k 1,1)

Here is my example output:

❯ LC_ALL=C df -lk --output=target,source,fstype,size,used,avail,pcent | (sed -u 1q; sort -k 1,1)
Mounted on                                                Filesystem     Type     1K-blocks      Used    Avail Use%
/                                                         /dev/nvme0n1p3 btrfs    241346700 140823752 98675592  59%
/boot/efi                                                 /dev/nvme0n1p1 vfat        510984       608   510376   1%
/dev                                                      dev            devtmpfs   8116048         0  8116048   0%
/dev/shm                                                  tmpfs          tmpfs      8130976    267196  7863780   4%
/home                                                     /dev/nvme0n1p3 btrfs    241346700 140823752 98675592  59%
/mnt/LAST                                                 /dev/nvme1n1p2 ext4     245069076 226165136  6382264  98%
/run                                                      run            tmpfs      8130976     93476  8037500   2%
/run/credentials/systemd-journald.service                 tmpfs          tmpfs         1024         0     1024   0%
/run/credentials/systemd-sysctl.service                   tmpfs          tmpfs         1024         0     1024   0%
/run/credentials/systemd-sysusers.service                 tmpfs          tmpfs         1024         0     1024   0%
/run/credentials/systemd-tmpfiles-setup-dev-early.service tmpfs          tmpfs         1024         0     1024   0%
/run/credentials/systemd-tmpfiles-setup-dev.service       tmpfs          tmpfs         1024         0     1024   0%
/run/credentials/systemd-tmpfiles-setup.service           tmpfs          tmpfs         1024         0     1024   0%
/run/credentials/systemd-udev-load-credentials.service    tmpfs          tmpfs         1024         0     1024   0%
/run/credentials/systemd-vconsole-setup.service           tmpfs          tmpfs         1024         0     1024   0%
/run/user/1000                                            tmpfs          tmpfs      1626192       144  1626048   1%
/sys/firmware/efi/efivars                                 efivarfs       efivarfs       100        70       26  73%
/tmp                                                      tmpfs          tmpfs      8130976     67652  8063324   1%
/var/cache                                                /dev/nvme0n1p3 btrfs    241346700 140823752 98675592  59%
/var/log                                                  /dev/nvme0n1p3 btrfs    241346700 140823752 98675592  59%

Of course you are able to modify it as you need exactly.

Piterden
  • 151
0

One liner:

df -h | python -c 'import sys; lines = sys.stdin.read().splitlines(); idx_start = lines[0].find("Mounted on"); lines[1:].sort(key=lambda x: x[idx_start:]); [print(line) for line in lines]'

Golfed (shortened):

df -h | python -c 'import sys;l=sys.stdin.read().splitlines();l[1:].sort(key=lambda x:x[l[0].find("Mou"):]);list(map(print,l))'

Expanded for readability:

df -h | python -c '
import sys
lines = sys.stdin.read().splitlines()
idx_start = lines[0].find("Mounted on")
lines[1:].sort(key=lambda x: x[idx_start:])
[print(line) for line in lines]
'

Finds what position the column "Mounted on" begins at, and then sorts from that position onwards.

This works with spaces.