How to get the percentage of memory free with a Linux command? [closed]

Using the free command:

% free
             total       used       free     shared    buffers     cached
Mem:       2061712     490924    1570788          0      60984     220236
-/+ buffers/cache:     209704    1852008
Swap:       587768          0     587768

Based on this output we grab the line with Mem and using awk pick specific fields for our computations.

This will report the percentage of memory in use

% free | grep Mem | awk '{print $3/$2 * 100.0}'
23.8171

This will report the percentage of memory that’s free

% free | grep Mem | awk '{print $4/$2 * 100.0}'
76.5013

You could create an alias for this command or put this into a tiny shell script. The specific output could be tailored to your needs using formatting commands for the print statement along these lines:

free | grep Mem | awk '{ printf("free: %.4f %\n", $4/$2 * 100.0) }'

Leave a Comment