Linux - How to calculate total diskspace in terminal, Covert Kilobytes to Megabytes or gigabytes of disk

How to calculate total diskspace in terminal

To find the disk size
 # fdisk -s /dev/sdb
   
   62914560
In the above command the disk space will be printed in KB. But I had the requirement to print the values in MB or GB for multiple servers.

To print diskspace in Megabytes or MB
 # echo "scale=2; $(sudo fdisk -s /dev/sdb) / 1024" | bc

To print diskspace in Gigabytes or GB
 # echo "scale=2; $(sudo fdisk -s /dev/sdb) / 1024^2" | bc


The output can be fetched using multiple ways
 
 # fdisk -s /dev/sda | awk '{$1=$1/1024; print $1,"MB";}'
To find the disk size in Gigabytes or GB
 # sudo fdisk -s /dev/sda | awk '{$1=$1/(1024^2); print $1,"GB";}'

Input file where total memory is stored in multiple files
 server1 MemTotal:       10122108 kB
 server2 MemTotal:       14258924 kB
 server3 MemTotal:       10122108 kB
 server4 MemTotal:       14258924 kB
 server5 MemTotal:       10122108 kB

To print output of memory stored in file
 # while read line; do awk '{print $1,$3=$3/(1024^2); }'  ; done<input
Below can be helpful when you have to parse or output from a file and print.

The topic on Linux - How to calculate total diskspace in terminal is posted by - Prathima

Hope you have enjoyed, Linux - How to calculate total diskspace in terminalThanks for your time

Tech Bluff