How can I *only* get the number of bytes available on a disk in bash?

Ryan picture Ryan · Oct 21, 2015 · Viewed 8.4k times · Source

df does a great job for an overview. But what if I want to set a variable in a shell script to the number of bytes available on a disk?

Example:

$ df
Filesystem            1K-blocks     Used Available Use% Mounted on
/dev/sda             1111111111  2222222  33333333  10% /
tmpfs                  44444444      555  66666666   1% /dev/shm

But I just want to return 33333333 (bytes available on /), not the whole df output.

Answer

Avinash Raj picture Avinash Raj · Oct 21, 2015

You may use awk,

df | awk '$1=="/dev/sda"{print $4}'