简体   繁体   中英

Linux Swap Usage Oneliner with numerical only output

I'd like to get a Linux bash one liner which will echo out the current SWAP in use (can be in kB, KB, MB). I only need the numbers, no text descriptions or any of the other RAM information... tried using Google, but it seems no one has requested this before.

A 2 - 5 line script would work as well, though I'll favor a one-liner...

This one liner will give the used swap based on subtracting the free swap from the total swap:

grep -E '^Swap[TF]' /proc/meminfo | awk 'p{print p-$2}{p=$2}'

The grep extracts the relevant lines from /proc/meminfo and the awk subtracts the two values.

edit: removed a $3 from the end of the awk print statement as the OP stated they just wanted the number. This number will be in KB.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM