Print Free and Total Disk of 'Real'? Partitions

Print Free and Total Disk of 'Real' Partitions

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?

Dave Crouse

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

要查看或添加评论,请登录

Cecil Westerhof的更多文章

  • Bash: Check Parameters

    Bash: Check Parameters

    EDIT 2023-03-08: Removed bug from nrOfFilesAndDirs I wanted to have the possibility to count in a directory the number…

  • Script To Add A Swap File

    Script To Add A Swap File

    A certain operation needed more as 20 GB of memory. Because my system only has 14 GB RAM and 9 GB swap that was a…

    1 条评论
  • Efficiently Calculating Multiplicative Persistence

    Efficiently Calculating Multiplicative Persistence

    Yesterday I participated in 'The SQLite & Tcl Conference 2021'. This brought into my mind an interesting problem I…

  • Exactly One Ace, King, Queen, Jack, Diamond, Hearth, Spade and Club in Rows, Columns and Diagonals

    Exactly One Ace, King, Queen, Jack, Diamond, Hearth, Spade and Club in Rows, Columns and Diagonals

    Today I saw this video: Euler Squares. This made me think: when we have the Aces, Kings, Queens and Jacks of a deck of…

  • Check, Update AND Rollback Pip Packages

    Check, Update AND Rollback Pip Packages

    Every year I learn at least one new language. This year it was Tcl (Tickle).

  • Tcl For System Scripts With Ease

    Tcl For System Scripts With Ease

    Introduction I want to learn new language every year. This year it was Tcl (Tickle).

    4 条评论
  • Simple Test Programs With Bash

    Simple Test Programs With Bash

    The problem There was a challenge to write a Java program that displays the number of connections for an undirected…

  • Maintaining PATH In Bash

    Maintaining PATH In Bash

    Introduction In Bash you ‘often’ have to add a directory to PATH. But sometimes this results in a contaminated PATH:…

  • Updating the Extension Pack of VirtualBox

    Updating the Extension Pack of VirtualBox

    When working with VirtualBox you have sometimes to update the extension pack. It is not difficult, but because it is…

  • Saving Netbook Battery With a Bash Script

    Saving Netbook Battery With a Bash Script

    In the script I am using a lower-as sign, but LinkedIn keeps throwing away the lower-as sign, so I substituted a ‘!’…

    1 条评论

社区洞察

其他会员也浏览了