9

is there a command in bash that can give you the total number of disk space/harddrive numbers.

I know the df command is very helpful but the output is too verbose:

    # df -h
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/sda4       721G  192G  492G  29% /
    tmpfs           129G  112K  129G   1% /dev/shm
    /dev/sda1       194M   92M   93M  50% /boot
    /dev/sdj1       917G  547M  870G   1% /data10
    /dev/sdk1       917G  214G  657G  25% /data11
    /dev/sdl1       917G  200M  871G   1% /data12
    /dev/sdm1       917G  200M  871G   1% /data13
    /dev/sdn1       917G  200M  871G   1% /data14
    /dev/sdo1       917G  200M  871G   1% /data15
    /dev/sdp1       917G   16G  855G   2% /data16
    /dev/sdb1       917G  4.6G  866G   1% /data2
    /dev/sdc1       917G   74G  797G   9% /data3
    /dev/sdd1       917G  200M  871G   1% /data4
    /dev/sde1       917G  200M  871G   1% /data5
    /dev/sdf1       917G  200M  871G   1% /data6
    /dev/sdg1       917G  764G  107G  88% /data7
    /dev/sdh1       917G   51G  820G   6% /data8
    /dev/sdi1       917G   19G  853G   3% /data9
    /dev/sda2       193G   53G  130G  30% /home
    cm_processes    129G   46M  129G   1% /var/run/cloudera-scm-agent/process

I basically want '16TB' in the end, is there a command handy or I have to write some program to calculate the total disk based on the output from df.

B.Mr.W.
  • 18,910
  • 35
  • 114
  • 178

3 Answers3

8

What about:

df --total

Hint: first look to the manual page: man df. I find it hard these days to find aspects for a program that have not been implemented by some nice flag. Linux people simply seem to know what programmers want/need.

Or if you only want the total:

df --total | tail -n 1

And if you want to specify it in a special blocksize (like TB), you can set the -B flag:

df --total -BT | tail -n 1

And in case you are only interested in the total size (for instance, you wish to use the result in another bash program):

df --total -BT | tail -n 1 | sed -E 's/total *([^ ]*).*/\1/'
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
4

Another solution using awk. This will print header and total lines:

df --total -h | awk '!/^\//'
  • awk command will print all lines except those who start with character /.
  • df with -h or --human-readable print sizes in powers of 1024 (e.g., 1023M).

The result will looks like the following:

Filesystem      Size  Used Avail Use% Mounted on
total           3.9T  1.7T  2.3T  42% -

As Mounted on field is useless, you can remove it by adding a sed to the previous command:

$ df --total -h | awk '!/^\//' | sed -E 's/Mounted on|\s-//'
Filesystem      Size  Used Avail Use%
total           3.9T  1.7T  2.3T  42%
jherran
  • 3,337
  • 8
  • 37
  • 54
3

CommuSoft's answer is nicer, but here is another solution using awk:

df -m | awk '{ SUM += $2} END { print SUM/1024/1024"TB" }'

Using the -m option make results from df be displayed in the same unit (MB). A simple division can translate the result to TB easily.

julienc
  • 19,087
  • 17
  • 82
  • 82