3

by the following command I capture available size from df

echo "$DF" | awk '$NF == "/var" { print $(NF-2) }' 

how to set the syntax if I want to set

NF == "/var"

or

NF == "/"

because I want to capture also the "/" additional to /var (sometimes /var is under / )

I tried:

 echo "$DF" | awk '$NF == "/var|/" { print $(NF-2) }'

but isn't working.

  • On a GNU system, you can also do df --output=avail / /var | tail -n +2 – Stéphane Chazelas Jun 2 '16 at 11:02
  • Or stat -fc '%a * %s / 1024' / /var | bc (still on a GNU system, also in KiB (for GNU df, whether it's in KiBs or half-KiBs depends on whether there's a POSIXLY_CORRECT variable in the environment, best it to add a -k option to make sure you always get KiBs)) – Stéphane Chazelas Jun 2 '16 at 11:10
  • FYI df --output=target,size / /var | uniq | awk 'NR>1 {print $2}' - with GNU df, that will print the size(s) of both / and /var if they are separate filesystems, or just / if not. The NR>1 in awk skips df's header line. – cas Jun 2 '16 at 11:56
3

You might do this:

echo "$DF" | awk '$NF == "/var" || $NF == "/" { print $(NF-2) }'

or if you want to use a regex, use the ~ regex matching operator:

echo "$DF" | awk '$NF ~ "^(/var|/)$" { print $(NF-2) }'

or:

echo "$DF" | awk '$NF ~ "^/(var)?$" { print $(NF-2) }'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.