0

I am trying to find available disk space through console

I found df -h | awk '$NF == "/" { print $4 }' to get required output.

[root@tmpe2etstmsdc01 ~]# df -h
Filesystem                                                 Size  Used Avail Use% Mounted on
/dev/mapper/vg_root-lv_root                                542G   22G  492G   5% /
tmpfs                                                      127G     0  127G   0% /dev/shm

Avail

[root@tmpe2etstmsdc01 ~]# df -h | awk '$NF == "/" { print $4 }'
492G

This gives Use% value when Filesystem is long

[root@ldrnr-tlsmsdc16 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_root-lv_root
                   16G   16G     0 100% /
tmpfs             1.9G     0  1.9G   0% /dev/shm

Here, the command gives Use% instead of Avail

[root@ldrnr-tlsmsdc16 ~]# df -h | awk '$NF == "/" { print $4 }'
100%

How can i get Avail in this condition?

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Venkatesh
  • 303
  • 1
  • 10
  • 22
  • Does this work to you? `df --output=source,avail`? See [How to select a particular column in linux df command](http://stackoverflow.com/a/28809214/1983854) – fedorqui Nov 18 '15 at 11:13
  • `df: unrecognized option '--output=source,avail'` `df: unrecognized option '--output=avail'` – Venkatesh Nov 18 '15 at 11:18
  • Must be a GNU option. It works on my `df (GNU coreutils) 8.22`. Are you in Bash? – fedorqui Nov 18 '15 at 11:21

1 Answers1

0

df -h | awk '$NF == "/" { if($1 ~ /^[0-9]/) print $3; else print $4 }'

or

df -h | awk '$NF == "/" { if($5 == "/") print $3; else print $4 }'

Venkatesh
  • 303
  • 1
  • 10
  • 22