[svlug] Question regarding top

Seth David Schoen schoen at loyalty.org
Mon Jun 12 16:16:28 PDT 2006

stripes writes:

> Hi all,
> I've looked around through /etc/toprc and ~/.toprc, and I can't find any
> way to format the output so it gives memory in MB (instead of kb).
> Is there anyway to do that on top, or do I need to use the free command
> (where I can).

You could modify the source code of top (it's in the procps package).
There doesn't seem to be an existing option in the source code to do

If you don't need to be portable to other Unixes, you might enjoy
something like

awk '{print $1, $2/1024, "MB"}' /proc/meminfo | egrep 'Used|Free'

You can get other values by adding things to your grep command.
You can also put this in a shell script and then call it from "watch"
or add your own loop like

while clear; do
awk '{print $1, $2/1024, "MB"}' /proc/meminfo | egrep 'Used|Free'
sleep 2

There are values that top has that this won't get you because you
would need to calculate FooTotal-FooFree to get FooUsed for certain
values of Foo.  This should be easy to do in awk.  For example:

awk '{values[$1] = $2/1024};
     END {print "SwapUsed:", values["SwapTotal:"] - values["SwapFree:"]}'

Seth David Schoen <schoen at loyalty.org> | This is a new focus for the security
     http://www.loyalty.org/~schoen/   | community. The actual user of the PC
     http://vitanuova.loyalty.org/     | [...] is the enemy.
                                       |          -- David Aucsmith, IDF 1999

More information about the svlug mailing list