Disk-Usage
如何正確輸出 df -k 命令
使用 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
使用
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 ...