0

we have the following df -h output

[root@trump var]# df -h | grep "/var"

/dev/mapper/vg-lvm_var   101G   85G   17G  84% /var

how to verify at least 10G free space on /var in bash/awk/perl one liner

meanwhile I write this syntax :

[[ ` df -h | grep "/var" | awk '{print $4}' | sed s'/G//g' ` -lt 10 ]] && echo "/var need at least 10G"

but this syntax isn't elegant and ugly and maybe I missed some rules

5
  • First of all df -BG /var | tail -n1 would be much more reliable an elegant as it will always display Giga Bytes
    – Kiwy
    Commented Mar 13, 2018 at 9:48
  • And I think your script works well and look relatively fine.
    – Kiwy
    Commented Mar 13, 2018 at 9:49
  • When you say 10G do you mean 10 GB or 10 GiB? They are different amounts and require the use of different flags on df or calculations within awk. (And yes, df is also incorrect in this regard.) Commented Mar 13, 2018 at 10:02
  • @roaima df -BG will output in giga bytes while -h will adapt depending on the size. it's just to have always the same behaviour depending on the drive.
    – Kiwy
    Commented Mar 13, 2018 at 10:05
  • @Kiwy, no that's where you're wrong. -BG is Gibibytes (2^n) and -BGB is Gigabytes (10^n). This confusion is exactly why I'm asking for clarification. Commented Mar 13, 2018 at 10:07

1 Answer 1

3

With a new enough GNU df, you could extract just the available space using the --output option. And you could the rest of the comparison with awk:

$ df -k --output=avail /var | awk 'NR == 2 && ($1 < 10*2**20) {print "/var need at least 10G"}'
/var need at least 10G

Even without that, if you wanted to get a specific filesystem, it's best to do df /var instead of df | grep /var (unless you want to catch the case of nothing being mounted on /var).

For running other commands based on this, with bash, use the (( )) arithmetic context:

if (( $(df -k --output=avail /var | tail -1) < 10*2**20 ))
then
    log "/var need at least 10G"
fi 
5
  • what in case we need to print the output by exe log function as the following df -k --output=avail /var | awk 'NR == 2 && ($1 < 10*2**20) log "/var need at least 10G" , then how to change the awk?
    – yael
    Commented Mar 13, 2018 at 10:00
  • yes but I only ask how to call function - log from awk in case we have less then 10G
    – yael
    Commented Mar 13, 2018 at 10:07
  • @Kiwy with that I'd need to strip the G. The -k prints in KB and without a suffix.
    – muru
    Commented Mar 13, 2018 at 10:08
  • @yael sorry, I wrongly tagged you in that comment. See update for running other commands instead.
    – muru
    Commented Mar 13, 2018 at 10:08
  • ok thx this is what we want
    – yael
    Commented Mar 13, 2018 at 10:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .