Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 am trying to get the usage-percentage for a given disk in a system. The program I have to write will be a bash shell script. The script has to work on Debian, Arch, and Mac OS X.

My current approach is to do a df and grep for the usage percentage. The problem is that the output of df differs on these systems. I want to avoid if os==Arch elseif os == Debian…as far as possible.

Is there a solution which avoids distinguishing the OS?

Example output for each operating system / version of df:


Filesystem    512-blocks       Used Available Capacity   iused    ifree %iused  Mounted on
/dev/disk2     179032136  106162416  72357720    60%  13334300  9044715   72%   /


Filesystem    1K-blocks  Used     Available  %used Mounted on
/dev/hdv1       20480000 8423436  11016564   44%   /


Filesystem     1K-blocks   Used Available Use% Mounted on
/dev/root        5160576 813420   4085012  17% /

So Arch and Debian are pretty the same. It may also be a problem that the labels are maybe printed in different languages.

share|improve this question
Could you post the df output on the different OSs so we can compare please? – terdon Apr 4 '13 at 10:53
Edited the post to show some examples. – k_wave Apr 4 '13 at 11:52
It isn't so much the OS but the source of the df utility. Most (all?) linux & BSD distributions use df from the GNU coreutils package. Mac OS X doesn't. OS X was originally built from a BSD-like interface onto its kernel but the utilities don't track closely with the GNU versions. – Doug Harris Apr 5 '13 at 14:28
up vote 0 down vote accepted

This should work for all three df outputs:

df | awk '/\//{print $1,$(NF-1)}'

This is the output on each of the examples you posted:

/dev/disk2 72%
/dev/hdv1 44%
/dev/root 17%


The awk command looks for lines that contain a / (so, ignores the headers) and prints the first ($1) and penultimate ($(NF-1)) fields. If you want only the usage percentage, do this:

df | awk '/\//{print $(NF-1)}'


My answer above will give you the %iused field on the Mac df output. If you want the Capacity instead, do this (works on all three again):

df | perl -ne '/\// && do {/(.+?)\s.+?(\d+%)/ && print "$1 $2\n"}'

which also prints the first field (the non-greedy (.+?), terminated by \s) and the first percentage number ((\d+%), preceded by the non-greedy .+?).

share|improve this answer
This does not work for the mac example, as the first number with % suffix is the capacity – k_wave Apr 5 '13 at 13:46
@kwave, that makes no difference. I am not using the % character anywhere. What I am doing is printing the next to last field which is always the percentage used. Did you try it? It works as advertised with the Mac output you have posted. – terdon Apr 5 '13 at 13:57
Hang on, in the Mac do you want the capacity or the inodes% ? Is that number ever different? Anyway, see my updated answer if you want the capacity and not the inodes. – terdon Apr 5 '13 at 14:15

A slightly messier but probably more universal solution is

df | sed -n 's/^\([^ ]*\) .*[^0-9]\([0-9]*%\).*/\1 \2/p'

which displays the first “word” on the line (the filesystem device name) and then looks for a sequence of digits followed by a %.  Specifically, it looks for the last such string.

[Oops: it will also display a mangled subset of the header (i.e., the first line, with the column headings).]


df | sed -n 's/^\([^ ]*\) .*[^0-9]\([0-9][0-9]*%\).*/\1 \2/p'

fixes the glitch with the header row, and

df | sed -n 's/^\([^ ]*\) [^%]*[^0-9]\([0-9][0-9]*%\).*/\1 \2/p'

captures the first percentage number field.  In particular, on the Mac, this will capture the Capacity column rather than the %iused column.

share|improve this answer
This does not work for the mac example, as the first number with % suffix is the capacity – k_wave Apr 5 '13 at 13:45
Oops. I fixed it. – Scott Apr 5 '13 at 16:42

Your Answer


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.