0

I need to get the ram size of the current machine and the used ram of the current machine in gb. How do i do this? I can get the ram size in kB, but how in gb?

RAM = $(grep MemTotal /proc/meminfo)

I would need one variable with the total ram and one with used ram. I am sorry for this question, but i am editing a bash script for the first time in my life right now ^^ Thanks for all of your Help! ;)

Cyrus
  • 84,225
  • 14
  • 89
  • 153
user15574677
  • 45
  • 1
  • 5

1 Answers1

5

Assuming you want Gibibytes (that is to say using 1024 bytes for Kb, 1024 Kb for Mb, etc), you can do the math yourself, though you'll have to filter the raw number out:

RAM_KB=$(grep MemTotal /proc/meminfo | awk '{print $2}')
RAM_MB=$(expr $RAM_KB / 1024)
RAM_GB=$(expr $RAM_MB / 1024)

You can do basically the same thing for used memory. Alternatively, many systems have the free utility installed that does this math for you:

free -g will provide the answers in GiB:

$ free -g
              total        used        free      shared  buff/cache   available
Mem:             24           0          23           0           1          23
Swap:             7           0           7

You can pull the values you need from there with grep and awk:

TOTAL_MEM=$(free -g | grep Mem: | awk '{print $2}')
MEM_USED=$(free -g | grep Mem: | awk '{print $3}')