Problem
Using a Linux command line, I’d want to obtain the available memory given as a percentage.
I tried the free command, but it only returns numbers with no choice for percentages.
Asked by Timothy Clemans
Solution #1
Using the command free:
% free
total used free shared buffers cached
Mem: 2061712 490924 1570788 0 60984 220236
-/+ buffers/cache: 209704 1852008
Swap: 587768 0 587768
We use Mem to grab the line and awk to choose specific fields for our computations based on this output.
This will report the percentage of memory in use
% free | grep Mem | awk '{print $3/$2 * 100.0}'
23.8171
This will show you how much RAM is available.
% free | grep Mem | awk '{print $4/$2 * 100.0}'
76.5013
Create an alias for this command or include it in a small shell script. Formatting directives for the print statement along these lines could be used to adapt the specific output to your needs:
free | grep Mem | awk '{ printf("free: %.4f %\n", $4/$2 * 100.0) }'
Answered by Levon
Post is based on https://stackoverflow.com/questions/10585978/how-to-get-the-percentage-of-memory-free-with-a-linux-command