Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux command for physical memory, getting the value only

Tags:

linux

shell

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?

like image 404
AabinGunz Avatar asked Jul 01 '11 09:07

AabinGunz


2 Answers

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
like image 128
4 revs Avatar answered Sep 23 '22 16:09

4 revs


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.

like image 33
Geoff Bucar Avatar answered Sep 26 '22 16:09

Geoff Bucar