Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
2  
What OS? What top variant? There are dozens of top implementations. –  Stéphane Chazelas Aug 13 '13 at 16:04
    
Please provide us your OS and top version (top -h). –  slm Aug 13 '13 at 17:09

2 Answers 2

[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
share|improve this answer
    
Which version of top is this? –  slm Aug 13 '13 at 16:40
    
I used centos-6.4. The top command on Ubuntu dont support -m for sorting my memory usage. –  damphat Aug 13 '13 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 '13 at 17:06
    
thanks @slm, i updated my post –  damphat Aug 13 '13 at 17:17

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".

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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