'How to display /proc/meminfo in Megabytes?
I want to thank you for helping me my related issue. I know if I do a cat /proc/meminfo
it will only display in kB. How can I display in MB? I really want to use cat
or awk
for this please.
Solution 1:[1]
This will convert any kB
lines to MB
:
awk '$3=="kB"{$2=$2/1024;$3="MB"} 1' /proc/meminfo | column -t
This version converts to gigabytes:
awk '$3=="kB"{$2=$2/1024^2;$3="GB";} 1' /proc/meminfo | column -t
For completeness, this will convert to MB or GB as appropriate:
awk '$3=="kB"{if ($2>1024^2){$2=$2/1024^2;$3="GB";} else if ($2>1024){$2=$2/1024;$3="MB";}} 1' /proc/meminfo | column -t
Solution 2:[2]
You can use the numfmt
tool
$ cat /proc/meminfo | numfmt --field 2 --from-unit=Ki --to-unit=Mi | sed 's/ kB/M/g'
MemTotal: 128692M
MemFree: 17759M
MemAvailable: 119792M
Buffers: 9724M
...
$ cat /proc/meminfo | numfmt --field 2 --from-unit=Ki --to=iec | sed 's/ kB//g'
MemTotal: 126G
MemFree: 18G
MemAvailable: 118G
Buffers: 9.5G
...
$ cat /proc/meminfo | numfmt --field 2 --from-unit=Ki --to-unit=Gi | sed 's/ kB/G/g'
MemTotal: 126G
MemFree: 18G
MemAvailable: 117G
Buffers: 10G
...
Solution 3:[3]
A single line bash entry for storing a memory quantity in MBs.
This is for MemTotal
but works for others like MemFree.
MEM_TOTAL_MB=`awk '/MemTotal/ {printf( "%d\n", $2 / 1024 )}' /proc/meminfo`
Notes:
backtick (`
) and single quote ('
) are used.
replace %d
with %.2f
if you want to display as a float with 2 decimal level precision.
Solution 4:[4]
Putting together John1024's answer and tips from shane's answer into function:
if [ -f "/proc/meminfo" ]; then
meminfo () {
__meminfo=$(awk '$3=="kB"{if ($2>1024^2){$2=$2/1024^2;$3="GB";} else if ($2>1024){$2=$2/1024;$3="MB";}} 1' /proc/meminfo)
echo "$__meminfo" | column -t;
unset __meminfo;
}
HW_TOTALRAM=$(meminfo | awk '/MemTotal/ {printf "%.2f", $2; print $3}')
fi
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | phuclv |
Solution 3 | phuclv |
Solution 4 | PotatoFarmer |