Linux

按掛載點對 df 輸出進行排序

  • May 28, 2021

我需要按其在列df -h中的掛載點按字母順序排序的輸出。Mounted on

但是,df -h目前的輸出類似於:

$ 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

有沒有像下面這樣輸出的命令?

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

使用 bash,您可以這樣做:

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

讀取(消耗)並列印第一行(標題),然後排序將消耗所有其餘部分。

引用自:https://serverfault.com/questions/1064987