Search code examples
linuxawkrhel

RHEL Release 5.5 (Tikanga), df --total option


I have a RHEL (Redhat Enterprise Linux) v6.5 (Santiago) server. On this server if i do a df -help there are list of options available. I am interested in the option --total However there is an older version of RHEL (v5.5). In which there is no --total option.

My question is, I have a command like this:

df -h --total  | grep total | awk 'NR==1{print$2}+NR==1{print$3}+NR==1{print$4}+NR==1{print$5}'

which gives the output as

62G
39G
21G
66%

Where

62G is Total size of the Disk
39G is Used
21G is remaining
61% Total usage %

The above command is working fine in RHEL v6.5. But fails in RHEL v5.5 since it does not have a --total option for df command.

When i run the same command on RHEL v5.5 i get the below error:

df: unrecognized option `--total'
Try `df --help' for more information.

So is there a command that can give me the output in the following way:

Total Disk Space
Used  Space
Remaining Disk space
Usage % 

Ex:

62G
39G
21G
66%

Solution

  • You'll have to do the calculation work yourself.

    Something like this awk script should work.

    $ cat dftotal.awk
    BEGIN {
        map[0] = "K"
        map[1] = "M"
        map[2] = "G"
        map[3] = "T"
    }
    function fmt(val,    c) {
        c=0
        while (val > 1024) {
            c++
            val = val / 1024
        }
        return val map[c]
    }
    
    {
        for (i=2;i<5;i++) {
            sum[i]+=$i
        }
    }
    
    END {
        print fmt(sum[2]) ORS fmt(sum[3]) ORS fmt(sum[4])
        print ((sum[3] / sum[2]) * 100) "%"
    }
    $ df -P | awk -f dftotal.awk