Oum Alaa
Oum Alaa

Reputation: 227

Get remaining disk space from df's output

I would like to get the remaining disk space in my home directory:

df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs           20G   15G  3.5G  81% /
/dev/root        20G   15G  3.5G  81% /
devtmpfs        990M  4.0K  990M   1% /dev
none            199M  2.7M  196M   2% /run
none            5.0M     0  5.0M   0% /run/lock
none            991M   12K  991M   1% /run/shm
/dev/sda2       894G  847G  1.9G 100% /home

Note: All I need from the output above is 1.9G

Upvotes: 0

Views: 232

Answers (5)

kenorb
kenorb

Reputation: 166319

Try one of the following commands:

$ grep -o 1.9G <(df -h)
$ df -h|awk '/home/{print $4}'
$ grep home <(df -h) | awk '{print $4}'
$ df=($(tail -1 <(df -h))) echo ${df[3]}
$ vi -es +'%j|norm 3dWWd$' -c%p -cq! <(df -h)
$ printf "%s\n" $(tail -1 <(df -h))|tail -3|head -1
$ ex -s <(df -h|egrep -o "\S+\s+\S+\s+/home") +'norm Wd$' +%p -cq!

Upvotes: 1

sehe
sehe

Reputation: 392833

While roundabout, and tied to ext2, here's a way with tune2fs

tune2fs /dev/sdc1 -l | egrep '^(Free blocks|Block size)' | 
    cut -d: -f2 | xargs |
    while read free size; 
    do 
        echo "$free blocks of size $size free: $((($size*$free)>>20)) mebibytes"
    done

Upvotes: 0

Nir Levy
Nir Levy

Reputation: 12953

use grep to take only the /home line, and awk to get only the field you want:

df -h | grep -w /home | awk '{print $4}'

Upvotes: 2

mklement0
mklement0

Reputation: 437062

Try the following awk command:

df -h | awk '$6 == "/home" { print $4 }'
  • $6 == "/home" only processes the (one) line whose 6th whitespace-separated column contains /home.
  • { print $4 } prints the 4th whitespace-separated field on that line, which is the remaining disk space.

Upvotes: 1

Tripp Kinetics
Tripp Kinetics

Reputation: 5439

Give this a try:

df -h | awk '{print $4}'

Upvotes: 0

Related Questions