How to capture disk usage percentage of a partition as an integer?
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.
command-line bash scripts disk-usage
|
show 2 more comments
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.
command-line bash scripts disk-usage
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'
– bc2946088
Nov 10 '16 at 11:25
I stand corrected, I can't find the switch to remove the header from df.
– bc2946088
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 considertr
, 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
|
show 2 more comments
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.
command-line bash scripts disk-usage
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.
command-line bash scripts disk-usage
command-line bash scripts disk-usage
asked Nov 10 '16 at 11:02
Arronical
13k84790
13k84790
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'
– bc2946088
Nov 10 '16 at 11:25
I stand corrected, I can't find the switch to remove the header from df.
– bc2946088
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 considertr
, 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
|
show 2 more comments
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'
– bc2946088
Nov 10 '16 at 11:25
I stand corrected, I can't find the switch to remove the header from df.
– bc2946088
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 considertr
, 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
1
1
why not simply parse it?
– Jacob Vlijm
Nov 10 '16 at 11:20
why not simply parse it?
– Jacob Vlijm
Nov 10 '16 at 11:20
1
1
I don't see a drawback either, you can remove the header with df then | tr -dc '0-9'
– bc2946088
Nov 10 '16 at 11:25
I don't see a drawback either, you can remove the header with df then | tr -dc '0-9'
– bc2946088
Nov 10 '16 at 11:25
I stand corrected, I can't find the switch to remove the header from df.
– bc2946088
Nov 10 '16 at 11:32
I stand corrected, I can't find the switch to remove the header from df.
– bc2946088
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
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
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'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
|
show 2 more comments
6 Answers
6
active
oldest
votes
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.
1
Usingtime
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 thansed
.
– Paddy Landau
Nov 15 '16 at 12:49
add a comment |
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}'
add a comment |
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
add a comment |
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
add a comment |
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.
add a comment |
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'
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f847752%2fhow-to-capture-disk-usage-percentage-of-a-partition-as-an-integer%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
1
Usingtime
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 thansed
.
– Paddy Landau
Nov 15 '16 at 12:49
add a comment |
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.
1
Usingtime
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 thansed
.
– Paddy Landau
Nov 15 '16 at 12:49
add a comment |
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.
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.
edited Nov 10 '16 at 12:04
muru
1
1
answered Nov 10 '16 at 11:47
bc2946088
3,18021129
3,18021129
1
Usingtime
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 thansed
.
– Paddy Landau
Nov 15 '16 at 12:49
add a comment |
1
Usingtime
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 thansed
.
– Paddy Landau
Nov 15 '16 at 12:49
1
1
Using
time
to test it comes out as being just as fast as sed.– Arronical
Nov 10 '16 at 11:54
Using
time
to test it comes out as being just as fast as sed.– Arronical
Nov 10 '16 at 11:54
4
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 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
@Arronical What muru said; invocation time is likely to dominate.
– a CVn
Nov 11 '16 at 13:23
1
1
In this instance,
tr
is easier to read than sed
.– Paddy Landau
Nov 15 '16 at 12:49
In this instance,
tr
is easier to read than sed
.– Paddy Landau
Nov 15 '16 at 12:49
add a comment |
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}'
add a comment |
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}'
add a comment |
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}'
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}'
edited Dec 5 at 19:18
answered Nov 10 '16 at 11:57
Sergiy Kolodyazhnyy
69.2k9144303
69.2k9144303
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Nov 10 '16 at 12:04
muru
1
1
answered Nov 10 '16 at 11:33
Zanna
49.9k13130237
49.9k13130237
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 11 '16 at 9:28
fedorqui
6,07611032
6,07611032
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Dec 6 at 4:12
WinEunuuchs2Unix
41.7k1070158
41.7k1070158
add a comment |
add a comment |
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'
add a comment |
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'
add a comment |
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'
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'
answered Jan 3 at 18:55
Ramon Fincken
11
11
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f847752%2fhow-to-capture-disk-usage-percentage-of-a-partition-as-an-integer%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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'
– bc2946088
Nov 10 '16 at 11:25
I stand corrected, I can't find the switch to remove the header from df.
– bc2946088
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