0

I know that free -m will print all the free and used memory values, including the values for Swap. However, I want to only get the numerical value for the amount of free swap on my system.

How can I get just the 'free swap' value from the free command?

3

From man free:

FILES
   /proc/meminfo
          memory information

You can parse that file directly instead of going via free:

$ awk '/SwapFree/' /proc/meminfo
SwapFree:        4007168 kB

Or, for only the value in MB:

$ awk '/SwapFree/ {print $2 / 1024}' /proc/meminfo
3913.25
0

I attempted to see if it was possible to do this without additional commands other than free -m, but I was unable to find such a solution.

This command, which can be run directly on the command line, can provide you the value of the "Free Swap" from free -m. You can put this into a script if you want to have a command you can call easily to run this:

free -m | grep "Swap" | awk '{ print $4 }'
4
  • Not really sure why you went and made this a script. This works as a one-liner command when run directly on the command line.
    – Thomas Ward
    Jan 26 '16 at 21:44
  • I made a script to store only. Jan 26 '16 at 21:47
  • 1
    there's no need for that in your answer - your original question didn't ask for a script. I've amended your answer so that it's a little more expanded, such that it has better grammar and says it can be run as a one-liner or added to a script. (I did similar revisions to your question, to make it a little better)
    – Thomas Ward
    Jan 26 '16 at 21:48
  • Get rid of grep, awk can search patterns Jan 26 '16 at 23:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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