0

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

mklement0
  • 382,024
  • 64
  • 607
  • 775
Oum Alaa
  • 227
  • 3
  • 11

5 Answers5

2

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}'
Nir Levy
  • 12,750
  • 3
  • 21
  • 38
  • Awesome, I am still beginner, if you could explain it to me please – Oum Alaa Oct 14 '15 at 02:51
  • 1
    @OumAlaa: `grep -w /home` only prints only lines from the output of `df -h` that contain the string `/home` as a whole word. The result is fed to `awk`, which then only prints the 4th whitespace-separated field on that line, which is the remaining disk-space amount. [explainshell.com](http://explainshell.com) is a great site for analyzing shell command lines; here's the explanation of `grep -w /home`: http://explainshell.com/explain?cmd=grep+-w+%2Fhome. – mklement0 Oct 14 '15 at 03:08
  • @OumAlaa: While not a real-world concern here in terms of performance, there is no need to get `grep` - and thus an extra process - involved, because `awk` can easily - and more accurately - perform the same test for the presence of `/home`, as evidenced by my answer. That said, it's sometimes easier to break the process into smaller steps, which is OK, as long as performance is not a concern. – mklement0 Oct 14 '15 at 03:10
  • Thanks a lot, appreciate it – Oum Alaa Oct 14 '15 at 22:51
1

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.
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Helpful, so kind of you, I am not familiar with "awk" – Oum Alaa Oct 14 '15 at 02:54
  • @OumAlaa: My pleasure; `awk` is well worth learning; it's a powerful text-processing tool - see https://en.wikipedia.org/wiki/AWK for background. – mklement0 Oct 14 '15 at 03:00
  • Thanks a lot, appreciate it – Oum Alaa Oct 14 '15 at 22:57
  • @OumAlaa: Because you've expressed appreciation of this particular answer without up-voting it, let me offer this _general_ advice: _in addition to accepting_ an answer, you can and are encouraged to _up-vote_ other answers that you find _helpful_, which helps future readers. (This is not a veiled request to up-vote _this_ answer.) – mklement0 Oct 18 '15 at 22:04
1

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!
kenorb
  • 155,785
  • 88
  • 678
  • 743
  • It's basically: `grep`/filter-by home <([process substitution](https://en.wikipedia.org/wiki/Process_substitution)) `|` `awk` {print 4th column} – kenorb Oct 14 '15 at 09:36
0

Give this a try:

df -h | awk '{print $4}'
Tripp Kinetics
  • 5,178
  • 2
  • 23
  • 37
  • This is the output: `df -h | awk '{print $4}' Avail 3.3G 3.3G 990M 196M 5.0M 991M 773M` but I need only the last one – Oum Alaa Oct 14 '15 at 22:56
0

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
sehe
  • 374,641
  • 47
  • 450
  • 633