How to capture disk usage percentage of a partition as an integer?
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 commandss/^ //
remove a space from the start of liness/%//
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 variableDF_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
Related videos on Youtube
Comments
-
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 ofdf
, but am happy to accept other methods that do not rely on it.-
Admin over 7 yearswhy not simply parse it?
-
Admin over 7 yearsI don't see a drawback either, you can remove the header with df then | tr -dc '0-9'
-
Admin over 7 yearsI stand corrected, I can't find the switch to remove the header from df.
-
Admin over 7 yearsI'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 over 7 years@bc2946088 printing only 0-9 with tr has the side effect of removing the header.
-
Admin over 7 yearsI 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 over 7 yearsI'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 over 7 yearsUsing
time
to test it comes out as being just as fast as sed. -
Admin over 7 years@Arronical unless your outputs are waaaaaaaaaaaay greater than 100%, I doubt you'd see much difference. :P
-
Admin over 7 years@Arronical What muru said; invocation time is likely to dominate.
-
Admin over 7 yearsIn this instance,
tr
is easier to read thansed
. -
SvennD about 5 yearsYou 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.