There are several questions and answers on askubuntu about seeing disk space from the command line.
Naturally, df
is the go-to tool for this.
However I want a script at login to raise an alert if the free disk space is below some threshold.
Unfortunately df
seems to use arbitrarily sized columns and I'm not sure how to extract just the value from the column I'm interested in.
For example:
$ df /dev/mapper/root
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/root 2563092 1649968 763212 69% /
$ df /dev/mapper/root | tail -n 1
/dev/mapper/root 2563092 1649968 763212 69% /
$ df /dev/mapper/root | tail -n 1 | cut -d ' ' -f 9
763212
Really I want the fourth column, yet I must ask for the ninth.
Is there an easier way to either extract the column's value from df
's output, or another way altogether to access this value.
Just realized that this isn't what was asked for, this shows an alert if the space used is greater than a predefined threshold. I'll leave it for further reference though.
You could use something like the script below
X holds the number of KB used, if greater than 999000 it emails a warning message.
I'd use an
awk
to only deal with the second line (skips the first here) and get you your column:If you're on Ubuntu 14.04 or later,
df
can output only the values you want it to:From
man df
:As noted by Drew Noakes, this ability was added in GNU
coreutils
8.21, and so isn't available in older versions of Ubuntu.For selecting fields,
awk
is a far better tool thancut
, and you can build on Jan's answer for that after picking the fields you wantdf
to output.