How to write Ram used to a file from a bash script

Viewed 255

I am wanting to write the amount of ram used to a file in a bash script.

if you run the command free you get the following output

             total       used       free     shared    buffers     cached
Mem:          7930       4103       3826          0         59       2060
-/+ buffers/cache:       1983       5946
Swap:        15487          0      15487

I am wanting to pull the used bit out and write to a file something like

MemUsed: 4103

I have tried varies of cat free | grep used' uniq >> ramInfo.txt but have been unable to get it correct.

I am completely new to shell scripts so forgive me if this is relatively simple.

1 Answers

You can do this and you will get the value:

free -h | awk '/^Mem/{print $4}'

You can also get the memory free in Kilobytes from /proc/meminfo:

cat /proc/meminfo | awk -F':' '/MemFree/{print $2}' | sed 's/^ *//g;s/ *$//g'
Related