How to capture disk usage percentage of a partition as an integer?

12,731

Solution 1

Here's awk solution:

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

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
 74

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

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

Solution 2

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

Solution 3

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]*'
83

Solution 4

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]/}
5
  • 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.

Solution 5

Here is an another solution:

df | grep /mount/point | awk '{ print $5 }' | cut -d'%' -f1
Share:
12,731

Related videos on Youtube

Arronical
Author by

Arronical

Always learning, normally trying not to offend.

Updated on September 18, 2022

Comments

  • Arronical
    Arronical over 1 year

    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:

    Use%
     83%
    

    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.

    • Admin
      Admin over 7 years
      why not simply parse it?
    • Admin
      Admin over 7 years
      I don't see a drawback either, you can remove the header with df then | tr -dc '0-9'
    • Admin
      Admin over 7 years
      I stand corrected, I can't find the switch to remove the header from df.
    • Admin
      Admin over 7 years
      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.
    • Admin
      Admin over 7 years
      @bc2946088 printing only 0-9 with tr has the side effect of removing the header.
    • Admin
      Admin over 7 years
      I assumed, I added it as an answer. I was on my phone and couldn't test it, so I didn't want to offer up a non-working answer at first.
    • Admin
      Admin over 7 years
      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.
  • Admin
    Admin over 7 years
    Using time to test it comes out as being just as fast as sed.
  • Admin
    Admin over 7 years
    @Arronical unless your outputs are waaaaaaaaaaaay greater than 100%, I doubt you'd see much difference. :P
  • Admin
    Admin over 7 years
    @Arronical What muru said; invocation time is likely to dominate.
  • Admin
    Admin over 7 years
    In this instance, tr is easier to read than sed.
  • SvennD
    SvennD about 5 years
    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.