Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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,

Steffen

share|improve this question

1 Answer 1

up vote 0 down vote accepted

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

#!/bin/sh
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}'
    shift
done

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:

#!/bin/sh
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:

#!/bin/sh
df --block-size=1G |\
awk '
    BEGIN {
        printf "%-5s%5s   %5s\n", "disk", "free", "total"
    }

    !match($1, "^/dev/([[:alpha:]]+)[[:digit:]]", 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]
            totalfree+=free[var]
            totalsize+=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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.