24

Can we get specific column in a top command, for eg I'm interested in only the memory utilization and CPU usage column.

How do I reduce the displayed columns of the top command to only the above two columns?

2
  • 3
    What OS? What top variant? There are dozens of top implementations. Aug 13, 2013 at 16:04
  • Please provide us your OS and top version (top -h).
    – slm
    Aug 13, 2013 at 17:09

5 Answers 5

24

NOTE: Assuming you have this version of top (procps). You can check with this command:

$ top --version
    top: procps version 3.2.8
usage:  top -hv | -bcisSH -d delay -n iterations [-u user | -U user] -p pid [,pid ...]

procps is often the version of top included with Fedora/CentOS/RHEL and other variants.

Changing columns

If you look in the man page for top you'll see a section titled: "2b. SELECTING and ORDERING Columns". There are keyboard shortcuts for toggling visibility for the different fields/columns.

For example:

  f,o     . Fields/Columns: 'f' add or remove; 'o' change display order
  F or O  . Select sort field
  <,>     . Move sort field: '<' next col left; '>' next col right

You can use the key f while in top to get to a secondary screen where you can specify which columns should be toggled visible or not:

For example:

Current Fields:  ANOPQRSTUVbcdefgjlmyzWHIKX  for window 3:Mem
Toggle fields via field letter, type any other key to return 

* A: PID        = Process Id                                      * W: S          = Process Status
* N: %MEM       = Memory usage (RES)                              * H: PR         = Priority
* O: VIRT       = Virtual Image (kb)                              * I: NI         = Nice value
* P: SWAP       = Swapped size (kb)                               * K: %CPU       = CPU usage
* Q: RES        = Resident size (kb)                              * X: COMMAND    = Command name/line
* R: CODE       = Code size (kb)
...

There are more, these are just a sample. When you're done toggling the columns the way you want, use the Esc to get out of the selection screen.

Saving configuration

You can use the Shift+W to save your changes so they're the defaults:

  W         Write configuration file

The file is stored here, $HOME/.toprc, and looks like this:

$ more .toprc 
RCfile for "top with windows"       # shameless braggin'
Id:a, Mode_altscr=0, Mode_irixps=1, Delay_time=1.000, Curwin=2
Def fieldscur=AEHIoqTWKNMBcdfgjpLrsuvyzX
    winflags=129016, sortindx=19, maxtasks=0
    summclr=2, msgsclr=5, headclr=7, taskclr=7
Job fieldscur=ABcefgjlrstuvyzMKNHIWOPQDX
    winflags=63416, sortindx=13, maxtasks=0
    summclr=6, msgsclr=6, headclr=7, taskclr=6
Mem fieldscur=ANOPQRSTUVbcdefgjlmyzWHIKX
    winflags=65464, sortindx=13, maxtasks=0
    summclr=5, msgsclr=5, headclr=4, taskclr=5
Usr fieldscur=ABDECGfhijlopqrstuvyzMKNWX
    winflags=65464, sortindx=12, maxtasks=0
    summclr=3, msgsclr=3, headclr=2, taskclr=7

See section 5 of the man page for more details, "5. FILES".

2
  • 4
    Is there a way to change the columns/field via a command line flag? This would be really useful, for instance for checking top on different machines and getting a standardised output.
    – naught101
    Dec 7, 2016 at 4:42
  • @naught101 - if you have a new question ask it on the main site. Comments aren't really meant for that.
    – slm
    Dec 7, 2016 at 22:54
3

[Centos-6 | Ubuntu 12.10] This cmd print 4 top processes sort by CPU USAGE

top -bn 1 | grep "^ " | awk '{ printf("%-8s  %-8s  %-8s\n", $9, $10, $12); }' | head -n 5

Output

%CPU      %MEM      COMMAND
7.7       0.2       top
0.0       0.3       init
0.0       0.0       kthreadd
0.0       0.0       migration/0

Note: head -n 5 instead of 4 because we also have column name

columns $9, $10, $12 means CPU, MEM, COMMAND. Use 'top' command to get column numbers

Sort by MEMORY USAGE (your 'top' must support -m to run this)

# this work on my centos-6 machine, NOT work on my Ubuntu 12.10
top -m -bn 1 | grep "^ " | awk '{ printf("%-8s  %-8s  %-8s\n", $9, $10, $12); }' | head -n 5
3
  • Which version of top is this?
    – slm
    Aug 13, 2013 at 16:40
  • I used centos-6.4. The top command on Ubuntu dont support -m for sorting my memory usage.
    – damphat
    Aug 13, 2013 at 17:01
  • Interesting, I'm on Fedora (14), I'm getting procps' version of top. I just double checked and on CentOS 5 + 6 they're using top procps as well. Your command didn't work on F14. It does work on CentOS 5 + 6 though. Just an FYI for anyone that comes across this thread!
    – slm
    Aug 13, 2013 at 17:06
1

to view in json format and removing header as well,

top -bn 1 | grep "^ " | awk '{ printf("%s%s%s\n","{CPU:"$9",","MEM:"$10",","CMD:"$12"}"); }' | head -n 6 | tail -n +2

output like below,

{CPU:6.4,MEM:0.3,CMD:gnome-terminal}
{CPU:6.4,MEM:1.9,CMD:chrome}
{CPU:0.0,MEM:0.0,CMD:init}
{CPU:0.0,MEM:0.0,CMD:kthreadd}
{CPU:0.0,MEM:0.0,CMD:ksoftirqd/0}
1
  • Your grep | awk | head | tail can be coalesced into a single awk. Aug 16, 2018 at 20:19
1
top -stats pid,cpu,threads,mem,purg,vsize,time,state

man top and look for -o for the list of supported keys (=columns).

0
0

Command line configuration
I did not want to affect my whole user by overwriting the user .toprc, so I came up with an solution to have a separate configuration per use-case.
You can make top use a custom configuration file instead of the one in your users home directory.
Just change the HOME variable to a custom directory containing the .toprc file for your needs.

# TOPRC_PROFILE_DIRECTORY contains .toprc (or can be created by saving using shift+w after configuring using keybindings) 
TOPRC_PROFILE_DIRECTORY="<your-directory>"
HOME="$TOPRC_PROFILE_DIRECTORY" top

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .