I would like a method to capture the disk usage of a particular partition, by using the directory where the partition is mounted. The output should just be an integer with no padding or following symbols, as I'd like to save it in a variable.

I've used df --output=pcent /mount/point, but need to trim the output as it has an unnecessary header, single space padding before the value, and a % symbol following the value like so:


In this case the output I would like would simply be 83. I'm not aware of any drawbacks to using the output of df, but am happy to accept other methods that do not rely on it.

  • 1
    why not simply parse it? – Jacob Vlijm Nov 10 '16 at 11:20
  • 1
    I don't see a drawback either, you can remove the header with df then | tr -dc '0-9' – user508889 Nov 10 '16 at 11:25
  • I stand corrected, I can't find the switch to remove the header from df. – user508889 Nov 10 '16 at 11:32
  • I'd read the man page, and the info page and couldn't find it either @bc2946088, good shout to consider tr, I was getting my head in a mess with sed and awk ideas. – Arronical Nov 10 '16 at 11:34
  • 3
    I've searched for removing header option,too. Basically GNU developers are reluctant to impleme it. There have been feature requests, and they just said no. – Sergiy Kolodyazhnyy Nov 10 '16 at 11:49

I'd use...

df --output=pcent /mount/point | tr -dc '0-9'

Not sure if sed is faster, but I can't ever remember the sed values.

| improve this answer | |
  • 1
    Using time to test it comes out as being just as fast as sed. – Arronical Nov 10 '16 at 11:54
  • 4
    @Arronical unless your outputs are waaaaaaaaaaaay greater than 100%, I doubt you'd see much difference. :P – muru Nov 10 '16 at 12:15
  • @Arronical What muru said; invocation time is likely to dominate. – a CVn Nov 11 '16 at 13:23
  • 1
    In this instance, tr is easier to read than sed. – Paddy Landau Nov 15 '16 at 12:49

Here's awk solution:

$ df --output=pcent /mnt/HDD | awk -F'%' 'NR==2{print $1}'   

Basically what happens here is that we treat '%' character as field separator ( column delimiter ), and print first column $1 only when number of records equals to two ( the NR==2 part )

If we wanted to use bash-only tools, we could do something like this:

bash-4.3$ df --output=pcent / | while IFS= read -r line; do 
>     ((c++)); 
>     [ $c -eq 2 ] && echo "${line%\%*}" ;
> done

And for fun, alternative sed via capture group and -r for extended regex:

df --output=pcent | sed -nr '/[[:digit:]]/{s/[[:space:]]+([[:digit:]]+)%/\1/;p}'
| improve this answer | |

sed solution

df --output=pcent /mount/point | sed '1d;s/^ //;s/%//'
  • 1d delete the first line
  • ; to separate commands
  • s/^ // remove a space from the start of lines
  • s/%// remove % sign
| improve this answer | |

You can pipe to a grep that just extracts digits:

df --output=pcent /mount/point | grep -o '[0-9]*'

See it live:

$ echo "Use%
> 83%" | grep -o '[0-9]*'
| improve this answer | |

I came upon a server where --output=pcent was not yet implemented, so I used the normal output, filtered by column, followed by the regex: df /mount/point | awk '{print $5}' | tr -dc '0-9'

| improve this answer | |
  • 1
    You should add -P or --portability to df; else if the /mount/point is to long it will line break and you get the wrong value. – SvennD Feb 15 '19 at 10:10

Bash two-step solution

Being somewhat of a bash (Borne Again SHell) fan the last year I thought I'd propose a solution using it.

$ DF_PCT=$(df --output=pcent /mnt/d)
$ echo ${DF_PCT//[!0-9]/}
  • Line 1 captures df output to variable DF_PCT.
  • Line 2 strips everything that is not a digit in DF_PCT and displays it on screen.
  • Advantage over accepted answer is line feed after percentage (5 in this case) is generated.
| improve this answer | |

Here is an another solution:

df | grep /mount/point | awk '{ print $5 }' | cut -d'%' -f1
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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