AabinGunz
AabinGunz

Reputation: 12347

Linux command for physical memory, getting the value only

In Linux cat /proc/meminfo | grep MemTotal gets back MemTotal: 12298824 kB

I want only the numbers here

so i wrote cat /proc/meminfo | grep MemTotal | cut -d':' -f2 which gave me 12298824 kB

I only want the numbers here, can anyone help me?

Note: cat /proc/meminfo | grep MemTotal | cut -d':' -f2 | cut -d'k' -f1 gives me the solution 12298824, but is there a better way? one liner?

Upvotes: 6

Views: 11290

Answers (2)

Geoff Bucar
Geoff Bucar

Reputation: 21

I have used:

dmidecode -t 17 | grep Size | awk '{s+=$2} END {print s}'

to great effect in my CentOS kickstart %pre section. It returns the total installed amount of memory in MB. Even if you have empty memory banks, awk will ignore them and only add the integer results.

Upvotes: 2

Prince John Wesley
Prince John Wesley

Reputation: 63708

Use awk:

cat /proc/meminfo | grep MemTotal | awk '{print $2}'

From @Lars Wirzenius's comment(No cat and No grep):

awk '/MemTotal/ { print $2 }' /proc/meminfo

Upvotes: 16

Related Questions