Print Free and Total Disk of 'Real' Partitions
Cecil Westerhof
Als trainer Persoonlijke Ontwikkeling help ik ECHT dienstbare ondernemers hun stress om te ruilen voor rust en plezier in hun leven.
Someone from the group got this code, but did not understand it:
????df -h | xargs | awk '{
????????print "Free/total disk: " $11 " / " $9
????}'
What I can understand, because it is blatantly wrong.
When executed this gives on my system:
????Free/total disk: 7.3G / 7.3G
The first two lines from 'df -h' on my system are:
????Filesystem?????????????Size?Used Avail Use% Mounted on
????udev???????????????????7.3G????0?7.3G??0% /dev
The header of 'df -h' contains (as far as awk is concerned) seven elements. ('Mounted on' is seen as two fields.) All other lines awk sees six fields. Size is the second field and Avail is the fourth field.
With xargs all the output is folded together as one line. Execute 'df -h | xargs | wc -l' to see this.
Because of this the fourth element of the second line becomes the eleventh. And the second element from of the second line becomes the ninth element. So the above code gives the free and total disk space of the first data line of 'df -h'.
I would think you would want this information from all 'real' partitions. This you get with:
????df -h | awk '
????????/^\// {
????????????printf "Free/total disk %-10s: %4s / %4s\n",
????????????????$6, $4, $2
????????}
????'
On my system this gives:
????Free/total disk /????????: 5.8G /?21G
????Free/total disk /tmp?????: 4.8G / 5.0G
????Free/total disk /boot????:?45M / 225M
????Free/total disk /boot/efi :?66M /?96M
????Free/total disk /var?????: 6.0G /?14G
????Free/total disk /home????:?54G / 736G
It would be better to sort the partitions:
????df -h | awk '
领英推荐
????????/^\// {
????????????printf "Free/total disk %-10s: %4s / %4s\n",
????????????????$6, $4, $2 | "sort -k 3,3"
????????}
????'
Which gives:
????Free/total disk /????????: 5.8G /?21G
????Free/total disk /boot????:?45M / 225M
????Free/total disk /boot/efi :?66M /?96M
????Free/total disk /home????:?54G / 736G
????Free/total disk /tmp?????: 4.8G / 5.0G
????Free/total disk /var?????: 6.0G /?14G
But it would be a good idea to also add some headers. For this we can use the BEGIN block of awk:
????df -h | awk '
????????BEGIN {
????????????print "???????????????Partition??Free??Total"
????????}
????????/^\// {
????????????printf "Free/total disk %-10s: %4s / %4s\n",
????????????????$6, $4, $2 | "sort -k 3,3"
????????}
????'
Which gives:
????????????????????Partition??Free??Total
????Free/total disk /????????: 5.8G /?21G
????Free/total disk /boot????:?45M / 225M
????Free/total disk /boot/efi :?66M /?96M
????Free/total disk /home????:?54G / 736G
????Free/total disk /tmp?????: 4.8G / 5.0G
????Free/total disk /var?????: 6.0G /?14G
I am curious what you think about this code and if you like me to share other code and what kind of code.
Is my explanation a bit sparse, or to broad?
Sr. Technical Administrator - Linux at Farm Bureau Financial Services
1 年Your explanation was great. I'm just not a fan of using awk unless i have no other choice :) df itself gives a multitude of options to print almost anything you want. Personally since i use xfs for almost all partitions, and I have several, I use: Formats out similar format to the op, df -hl --output=target,avail,size -t xfs --total????? Mounted on????Avail?Size /??????????????4.2G?6.8G /home??????????1.9G?2.0G /opt???????????975M 1014M /var???????????1.4G?2.0G /tmp???????????2.0G?2.0G /var/tmp???????892M 1009M /var/log???????9.9G??10G /var/log/audit?972M 1014M total???????????22G??26G