Search code examples
linuxmacoscommanddiskspace

df --total command for mac?


I want to use command line to retrieve the total disk space on a machine as one value. On most Linux machines, the "df --total" returns such a value at the end of the regular df output.

On osx9, 10, 11, and 12, there seems to be no option like this.

[u'df: illegal option -- -\n', u'usage: df [-b | -H | -h | -k | -m | -g | -P] [-ailn] [-T type] [-t] [filesystem ...]\n']

Same issue with older linux os, redhat5, centos5, etc.

Another way to approach this? Is there another command to isolate certain values? I am mainly looking for the "total% used" value.


Solution

  • You'll have to calculate the value yourself, e.g. using awk:

    df | awk '
        BEGIN { SIZE=0; USED=0; AVAILABLE=0 }
        // { print }
        $3~/[0-9]/ { SIZE += $2; USED += $3; AVAILABLE += $4 }
        END { printf "total\t%lu\t%lu\t%lu\t%d%%\n", SIZE, USED, AVAILABLE, (USED * 100 / SIZE); }'
    

    to break it down

    • line 1 of awk sets up the variables at the start,
    • line 2 prints every line (I'm making it explicit),
    • line 3 says if I find a number in the third column add it to the totals
    • line 4 says at the end print a 'total' line (somewhat similar to --total's line)