Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

As a output of free -m command I got following.

             total       used       free     shared    buffers     cached
Mem:          2496       2260        236          0          5        438
-/+ buffers/cache:       1816        680
Swap:         1949         68       1881

I want to get only used Memory like 2260 as output. I tried following command.

free -m | grep Mem | cut -f1 -d " " 

Help me to improve my command.

How can I get it in percentage like 35% ?

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

As for the added question of displaying as percentage (based on jasonwryan's answer):

awk '/^Mem/ {printf("%u%%", 100*$3/$2);}' <(free -m)

get percentage by diving 3rd field by 2nd and print as an integer (no rounding up!).

EDIT: added double '%' in printf (the first one escapes the literal character intended for printing).

share|improve this answer
    
@user34571 thanks for pointing out the double % issue. You may also want to put these things into comment next time (to get some credits :)). –  peterph Mar 20 '13 at 10:09
    
Thank You Friend –  K.K Patel Mar 20 '13 at 11:39
add comment

You can use awk without the need for a separate grep pipe for this:

awk '/^Mem/ {print $3}' <(free -m)

Where records/rows are filtered for those beginning with Mem and the third field/column ($3) is printed for the filtered record.

share|improve this answer
    
Thanks a lot !! –  K.K Patel Mar 20 '13 at 8:27
add comment

Or with sed:

free -m | sed -n 's/^Mem:\s\+[0-9]\+\s\+\([0-9]\+\)\s.\+/\1/p'

Another solution would be:

free -m  | grep ^Mem | tr -s ' ' | cut -d ' ' -f 3

Credits for the second solution got to this post.

share|improve this answer
    
It gives me total memory . –  K.K Patel Mar 20 '13 at 8:30
    
Yeah, sorry. Fixed. :) –  0x80 Mar 20 '13 at 8:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.