A Detailed Guide to the Unix/Linux `top` Command
¶ TOP Overview
The top command is one of the most fundamental tools on Unix/Linux. It is similar to Windows Task Manager and lets you monitor the execution status of currently running programs. top is also one of the simplest ways to monitor a program: you can use it to observe how much memory and CPU a program consumes, along with many other details. There are many similar tools (such as htop and gtop) which are extended versions. If you’re interested, you can look them up, but for basic needs, top is more than sufficient.
Using it is straightforward:
$ top

By default, the displayed columns include:
PID: Process IDUSER: The user running the taskPR: Task priorityNI: Nice value; negative means higher priority, positive means lower priorityVIRT: Total virtual memory used (kB)RES: Resident memory size (kB)SHR: Total shared memory used (kB)S: Status- R: running
- D: uninterruptible sleep (typically waiting for I/O; cannot be interrupted by signals)
- S: sleep (interruptible / can be woken)
- T: stopped or traced; may be stopped by
SIGSTOP/SIGTSTP, or traced (e.g., by a debugger via ptrace) - Z: zombie; usually occurs when the child has finished and is waiting for the parent to wait()/reap it
%CPU: CPU usage percentage. Note that one core is 100%, so on multicore systems this can exceed 100%.%MEM: Percentage of total memory usedTIME+: Total CPU time usedCOMMAND: Command name
¶ TOP Interactive Mode
One way to use top is to enter the interactive UI and operate it there. Below I list some of the most commonly used keys. For the rest, please refer to the links in 參考資料. The recommended way to learn is to read the article while trying the operations yourself.
¶ Sorting tasks
Sorting tasks is probably the most important operation, because the common use case of top is to see which processes are consuming the most CPU and memory.
M: sort by memory usageN: sort by PIDP: sort by CPU usageT: sort by running time>/<: move the sort column to the right/leftR: reverse sort
¶ General operations
The key thing here is: press q to quit 😂
h/?: help- Enter/Space: refresh the screen (default: every 3 seconds)
=: clear task filters (after searching)B: bold displayd/s: change refresh intervalI: toggle between showing CPU usage percentage / showing all CPU coresk: kill a specific PIDL: search for a string (use with&)q: quitr: renice a PID (positive values lower priority, negative values raise priority; requires sudo)u/U: filter by userW: write current settings (they persist next time)Z: change colors
¶ Top panel settings
l: show/hide uptimem: show/hide memoryt: show/hide tasks (CPU summary)1: show/hide per-core values
¶ Task panel settings
Press f to see more system fields. Combined with b, x, and y, it becomes easier to see what’s going on.
b: highlight the task panelx: highlight the selected column (requiresb)y: highlight the selected row / running task (requiresb)c: show full command linef: configure which fields to display (there are many)d: toggle display- Right arrow: adjust ordering (use up/down keys)
- Left arrow: cancel ordering
esc/q: exit configuration
i: hide idle tasksn/#: set and show how many tasksj: right-align columns
¶ Example
Below is what it looks like after sorting by CPU (P) and enabling highlighting with b, x, and y:

Help text shown by pressing h:

¶ TOP Command-Line Options
top also supports command-line options:
$ top -h
top -hv | -bcHisS -d delay -n limit -u|U user | -p pid -w [cols]
Here I’ll highlight the most important ones; for detailed parameter descriptions, please see the 參考資料 links.
For example, this shows the top 10 tasks for the user acliu:
$ top -n 10 -u acliu
One useful trick is to use batch mode (option -b) together with other shell tools. For example:
$ top -b -n 3 | grep "python"
This output extracts the lines containing “python” from top. Also, because we set -n (in batch mode, this is the number of iterations), it will print the batch output three times (one per refresh).
¶ 參考資料