Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I wonder if there is a way to get the capacity of a disk as a numeric value in FreeBSD.

I don't want to use df because it doesn't return a single numeric value, du -sch / takes too much time summing up.

Maybe there is a variable accessible via sysctl? I tried sysctl -a -d | grep -e disk but havn't found something.

Thanks in advance,


share|improve this question
up vote 0 down vote accepted

A quick sketch of an awk physical disk usage printer (nb: only tried on Linux):

while ! [ $# -eq 0 ]; do
    df --block-size=1G | awk '$1 ~ /\/dev\/'"${1}"'/ {free+=$4; total+=$2} END {printf "%4s: %4sG / %4sG\n", "'"${1}"'", free, total}'

Save the above script as e.g. dfsum and run it as e.g.:

$ dfsum sda sdb
sda:   19G /  916G
sdb:   59G / 1834G

EDIT: An updated version with a header and automatic detection of disks:

df --block-size=1G | awk '
    BEGIN {printf "%-5s%5s   %5s\n", "disk", "free", "total"}
    !match($1, "^/dev/([[:alpha:]]+)", disk) {next} \
    {free[disk[1]]+=$4; total[disk[1]]+=$2} \
    END {for (var in free) printf "%3s: %4sG / %4sG\n", var, free[var], total[var]}'

Note: this requires gawk.

EDIT again:

df --block-size=1G |\
awk '
    BEGIN {
        printf "%-5s%5s   %5s\n", "disk", "free", "total"

    !match($1, "^/dev/([[:alpha:]]+)[[:digit:]]", disk) {next} 


    END {
        for (var in free) {
            printf "%3s: %4sG / %4sG\n", var, free[var], total[var]
        printf "     -------------\n     %4sG / %4sG\n", totalfree, totalsize

This also gives a total sum, and fixes a change in my system that occured after a restart where a column with /dev/disk/by-uuid/... is shown for the root filesystem in the df output, but also as a separate entry as /dev/sda1. I'm only interested in the latter.

share|improve this answer
Yes, awk would be possible. I am looking for a not-scripted solution where the value comes right out of the system (df has to store the value somewhere, right?). But, if there is none, what I suppose, I will use awk. – Steffen Mar 14 '12 at 11:06
Well, the value from df is per partition. It doesn't really make sense to store a "physical free space" value, since it is dependent on file system, block size, reserved space, etc. of the separate partitions, so a "physical" value would have to take the separate partition environments into account anyway. Thus the best one could store would be a continuously updated sum of the partition values, which is what awk gives you. That is why I don't think there is a system-wide value stored somewhere. – Daniel Andersson Mar 14 '12 at 14:13
True. So, I say Helloto awk. Thanks :) – Steffen Mar 14 '12 at 14:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .