Skip to main content
2 of 4
added 164 characters in body
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k

You could do something like:

LC_ALL=C free -h | awk ' /^Mem/ { suffix = $2 sub(/[0-9.]*/, "", suffix) printf "%.0f%sB\n", $2, suffix }' 

(LC_ALL=C to make sure the numbers are printed using the . decimal radix (3.7G would be output as 3,7G in locales using comma as the decimal radix)).

On GNU/Linux systems, you can also do:

awk '/^MemTotal/{print $2*1024}' < /proc/meminfo | numfmt --to=iec --format=%.0f --suffix=B 

Or:

free -h | awk '/^Mem/{print $2}' | numfmt --from=iec --to=iec --format=%.0f --suffix=B 

(that one coping with locales where the decimal radix is not .).

Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k