![如何获得 df -k 命令的正确输出](https://linux22.com/image/81705/%E5%A6%82%E4%BD%95%E8%8E%B7%E5%BE%97%20df%20-k%20%E5%91%BD%E4%BB%A4%E7%9A%84%E6%AD%A3%E7%A1%AE%E8%BE%93%E5%87%BA.png)
我在使用 df -k 命令时遇到一个问题。我需要结果采用正确的格式,以便我可以将其用于其他目的。
系统 1 - 我使用了这个命令( df -k | tr -s " " )
输出1
$ df -k | tr -s " "
Filesystem 1024-blocks Free %Used Iused %Iused Mounted on
/dev/hd4 1703936 1317848 23% 7154 1% /
/dev/hd2 4849664 2106456 57% 57120 5% /usr
/dev/hd9var 3670016 939564 75% 16242 2% /var
/dev/hd3 3932160 3787604 4% 382 1% /tmp
/dev/hd1 5242880 1046220 81% 71598 6% /home
/proc - - - - - /proc
/dev/hd10opt 2228224 440928 81% 17096 4% /opt
/dev/ecclv 1048576 165284 85% 2928 2% /opt/ecc
/dev/metriclv 131072 96768 27% 102 1% /var/adm/metric
/dev/perflv 262144 120840 54% 80 1% /var/opt/perf
/dev/carhome_lv 10485760 7249912 31% 264574 15% /xcarapps/prod
/dev/cdnhome_lv 59572224 11050440 82% 2317 1% /apps5
/dev/ccds_lv 27230208 21092196 23% 1075858 19% /apps2
/dev/appslv 2293760 1848688 20% 938 1% /apps
系统 2 - 我使用了这个命令( df -k | tr -s " " )
输出2
$ df -k | tr -s " "
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/vg00-lvroot
2789856 384228 2263908 15% /
udev 99240232 600 99239632 1% /dev
tmpfs 99240232 32 99240200 1% /dev/shm
/dev/cciss/c0d0p1 1035660 81632 901420 9% /boot
/dev/mapper/vg00-lvhome
690217 441040 213557 68% /home
/dev/mapper/vg00-lvopt
3096336 2189872 749180 75% /opt
/dev/mapper/vg00-lvnetbk60
4128448 1048232 2870504 27% /opt/openv
但我需要类似于输出 1 的上述输出(我的意思是在正确的行中,如下所示)
所需输出
$ df -k | tr -s " "
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/vg00-lvroot 2789856 384228 2263908 15% /
udev 99240232 600 99239632 1% /dev
tmpfs 99240232 32 99240200 1% /dev/shm
/dev/cciss/c0d0p1 1035660 81632 901420 9% /boot
/dev/mapper/vg00-lvhome 690217 441040 213557 68% /home
/dev/mapper/vg00-lvopt 3096336 2189872 749180 75% /opt
/dev/mapper/vg00-lvnetbk60 4128448 1048232 2870504 27% /opt/openv
为什么我需要这个,我需要使用下面的代码将输出写入 TXT 文档中。它适用于“系统 1”,但由于格式问题而无法为“系统 2”写入。
将系统 1 的输出写入 TXT 文件的正确代码
unix_space=`df -k | tr -s " " | awk '{if (substr($4,1,(length($4)-1)) >=92) printf "%-30s\n",$1;}'|wc -l`
df -k | tr -s " " | awk 'BEGIN {print"##############################################################################\n File System Total Space Free space used% Mounted Directory \n##############################################################################\n"} {if (substr($4,1,(length($4)-1)) >=92) printf "%-30s|%-12s|%-12s|%-7s|%-30s\n",$1,$2,$3,$4,$7;}' > ${DATA}/logs/UNIXSPACE.txt
答案1
使用 df -P
:
-P, --portability use the POSIX output format
$ df -P
Filesystem 1024-blocks Used Available Capacity Mounted on
/dev/mapper/vg00-lv_root 14987656 4220264 9999392 30% /
tmpfs 4026908 60932 3965976 2% /dev/shm
/dev/sda1 487652 36259 425793 8% /boot
/dev/mapper/vg00-lv_tmp 5916420 47636 5561584 1% /tmp
/dev/mapper/vg00-lv_var 20027260 1683820 17319440 9% /var
...