Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Could you help me to modify the following one-liner to get more precise memory usage - right now it's just an integer I would like to include first value after coma.

free -m | awk '/Mem:/ {tot = $2;} /cache:/{printf "%d\n", $3 / tot * 100}'

Thank you

share|improve this question
printf "%.1f\n" ? – ripat Jun 6 '12 at 19:33
up vote 5 down vote accepted

Your %d format specifier is forcing printf to print an integer value. If you change it to %f then it would print the full number. You can use to limit the number of decimal places printed so %.1f would print one decimal place.

share|improve this answer
Already posted above, although with less explanations ^_^ – ripat Jun 6 '12 at 19:49
@ripat Should have made it an answer :). If the correct answer is in a comment, then it's fair game to be "reposted" as an answer, since an OP cannot mark a comment as "accepted". – MDMarra Jun 6 '12 at 19:56
@MDMarra. I agree. My apologies but the answer was so short and obvious... – ripat Jun 7 '12 at 14:49
@ripat: obvious to you and I because we know the answer but clearly not obvious to everyone. As you can see, answers don't have to be short either, I think longer is better. – Iain Jun 7 '12 at 14:52

Your Answer


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.