top Command in Linux: Syntax, Options, Examples

October 30, 2024

Introduction

The top (table of processes) command shows a dynamic, real-time view of running processes and kernel-managed tasks in Linux. The command also provides a system information summary that shows resource utilization, including CPU and memory usage.

In this tutorial, you will learn to use the top command in Linux and see some practical examples.

How to use the Linux top command - tutorial.

Prerequisites

  • A system running Linux.
  • Access to a terminal window.
  • Administrator/sudo privileges.

top Command Syntax

The top command has the following basic syntax:

top [options]

Run top without any options to display a live view of all processes running on the system:

top
Example of the top command output.

The command starts in interactive mode, showing the active processes and other system information. Customize the view using the available options. Press q to exit top.

top Command Options

The options modify the command's behavior and output. They affect what processes are shown, how often the information is updated, and which resources are highlighted. Either specify the options when running top in the terminal or use the designated hotkey (if available) once you are in the top interactive mode.

Note: Hotkeys and options are case-sensitive.

The table below lists the most commonly used top command options and hotkeys and their descriptions:

OptionNameHotkeyDescription
-hHelpN/AShows the command help file.
-vVersionN/AShows the program version and usage prompt and then quits the program.
-bBatch modeN/AStarts top in batch mode, allowing users to send the command's output to a file or other programs. By default, it does not accept input in batch mode and runs until it is killed or reaches the specified iteration limit.
-cToggle command line/program namecReverses the last remembered c state. If top displayed command lines, that field now shows program names and vice versa.
-d [secs.tenths]Delay time intervalN/APrompts you to specify the delay before updating the screen, overriding the default value. The value is in seconds. The command accepts fractional seconds but does not accept negative numbers.
-e [k | m | g | t | p]Toggle task memory scalingeAllows you to change top task area memory scaling. The available scaling options are k (kibibytes), m (mebibytes), g (gibibytes), t (tebibytes), p (pebibytes).
-E [k | m | g | t | p | e]Toggle summary memory scalingEAllows you to change top summary area memory scaling. The available scaling options are k (kibibytes), m (mebibytes), g (gibibytes), t (tebibytes), p (pebibytes), e (exbibytes).
-HThreads mode operationHDisplays individual threads instead of a sum of all threads in each process.
-iIdle process toggleiReverses the last remembered i state. When the toggle is OFF, top does not display the tasks that haven't used CPU resources since the last update.
-nLimit iteration numberN/ASpecify the maximum number of iterations you want top to produce before ending.
-o [field name]Overwrite sort fieldoSpecify the field name top uses to sort tasks. Prepending a + to the field name forces top to sort high to low while prepending it with - orders tasks low to high. The -o option primarily supports automated batch mode operation.
-OOutput field namesN/AThe -O option complements the -o option and instructs top to print each of the available field names separately and then quit.
-p [N1, N2...]Monitor process IDs modeN/AInstructs top to monitor only the processes with the specified process IDs (PIDs). When combined with the threads mode (-H), top shows up to 20 processes in each monitored PID thread group. Specifying the PID value 0 is considered as the top program process ID. To return to normal operation, issue any of the following commands: =, u, or U, as they are mutually exclusive.
-sSecure mode operationN/AStarts top in secure mode, even for root.
-SCumulative time toggleSReverses the last remembered S state. When the cumulative time toggle is ON, each process is listed with the CPU time it has used.
-u | -U [ID or name]User filter modeuDisplays only the processes matching the specified user ID or user name. The -u option matches only the effective user, while the -U option matches any user - real, effective, saved, or filesystem. Prepending the user ID or user name with an exclamation point (!) causes top to display only the processes that do not match the one provided.
-w [number]Output width overrideN/AWhen used without an argument, -w instructs top to format the output using the COLUMNS= and LINES= environment variables, if applicable. Otherwise, the width is fixed at 512 columns maximum. Appending an argument to -w increases (up to 512) or decreases the output width while the row number is unlimited. The program never exceeds the actual terminal dimensions at which top was invoked.
-1Single/separate CPU states toggle1Reverses the last remembered CPU state portion. Depending on the NUMA Node command toggle (2) state, it either displays all CPU information in a single line or separately.
N/ASort by CPU usagePSorts the tasks by CPU usage.
N/ASort by memory usageMSorts the tasks by memory (%MEM) usage.
N/ASort by process IDNSorts tasks by process ID.
N/ASort by running timeTSorts the tasks by running time.
N/AToggle ascending/descending orderRReverses the sort order from descending to ascending.

How to Interpret top Command Output

The top command output is divided into several sections, each with specific information about system performance and processes. This section provides a breakdown of the output based on the information it shows.

Header Information

This section shows basic system stats like uptime, active users, and recent system load:

  • up. Displays the system running time since the last reboot.
  • user(s). Shows the current number of active users.
  • load average. Lists the average system load over the last 1, 5, and 15 minutes. Lower values indicate less CPU demand.
Header information in the top command output.

Tasks

The Tasks section provides a quick overview of all running processes and their states:

  • total. Indicates the number of processes currently running on the system.
  • running. Number of processes actively using CPU resources.
  • sleeping. Processes waiting for an event to continue (e.g., input/output).
  • stopped. Processes that are paused or waiting for signals.
  • zombie. Processes that have finished execution but remain in the process table.
Task information from top output.

CPU Usage

This section shows how various processes are using the CPU.

  • us. Time the CPU spends on user (non-kernel) processes.
  • sy. Time spent on system/kernel processes.
  • ni. Time spent on user processes with adjusted priority (nice values).
  • id. Percentage of time the CPU is idle.
  • wa. Time spent waiting for I/O operations (like disk access).
  • hi. Time handling hardware interrupts.
  • si. Time handling software interrupts.
  • st. Time "stolen" from the virtual machine by the hypervisor (when using virtualization).
CPU usage information.

Memory Usage (Mem)

Memory usage stats explain how the system uses physical RAM.

  • total. Total physical memory available.
  • free. Unused RAM memory.
  • used. Total memory in use by the system.
  • buff/cache. Memory reserved for buffers and caches. This memory can be reclaimed if needed by other processes.
RAM memory usage in top.

Swap Usage (Swap)

The Swap section shows the disk-based memory usage, which the system uses when RAM is full.

  • total. Total swap memory (disk-based memory used when RAM is full).
  • free. Unused swap memory.
  • used. Swap currently in use.
  • avail Mem. Total memory available (sum of free and buffered memory).
Swap memory usage information.

Process List

The Process List displays detailed information about each active process.

  • PID. The process ID.
  • USER. The user account that started the process.
  • PR. Process priority.
  • NI. Nice value, affecting scheduling priority.
  • VIRT. Total virtual memory the process uses.
  • RES. Resident memory (actual physical memory in use).
  • SHR. Shared memory used by the process.
  • S. Process state (e.g., S for sleeping, R for running).
  • %CPU. Percentage of CPU time used by the process.
  • %MEM. Percentage of RAM used by the process.
  • TIME+. Total CPU time the process has consumed.
  • COMMAND. The program, service, or command responsible for each active process.
Process list in top command output.

top Command Examples

Below are the most common top command use cases. The options are case-sensitive, which means that pressing N and n invokes a different action.

Note: Use the top command options by pressing the corresponding key while top is running, or enter the option flag directly in the top command syntax and run it from the command line.

List All Processes

To list all running Linux processes on your system, open the terminal and enter:

top
An example output of the top command in Linux.

The output displays the summary area (the dashboard with resource usage stats) and the task area (a list of all processes). top updates the information every three seconds by default.

If the process list is long, scroll through it using the Up and Down arrows and Page Up and Page Down keys. Press q to quit top.

Send a Signal

Use the top command to send any signal to a running process. Press the k key and enter the process PID. top prompts you to type the signal you want to send. Not entering a specific signal kills the process.

For example:

Sending a signal / killing a process in Linux.

Here, we killed the mysql process 1207.

Note: See other ways to kill a process to increase system performance. And, if you need to allow processes to complete even after logging out, check out our post How to Use Linux nohup Command.

Exit After X Repetitions

The top command keeps refreshing the statistics until you quit the program with q. Use the following syntax to quit top automatically after refreshing the stats for a specified number of times:

top -n [X]

For [X], specify the number of times you want top to refresh the output. For example, to refresh the output 5 times, run:

top -n 5

Sort Processes

By default, top sorts the process list using the %CPU column. To sort processes using a different column, press one of the following keys:

  • M. Sort by the %MEM column.
  • N. Sort by PID column.
  • T. Sort by the TIME+ column.
  • P. Sort by the %CPU column.

In the following example, the process list is sorted by the %MEM column:

Sort processes in the top command by columns.

Note: Make the settings and customizations persistent by pressing the W key. The changes will be in effect the next time you start top. The configuration is kept in the .toprc file in your home directory.

Filter Processes by Specific User

The -u option allows you to display all user-specific processes. Press the u key while top is running or use the following syntax when you run top:

top -u [user name]

Pressing the u key in interactive mode prompts for the user name or user ID.

For example:

Show specific user processes in Linux.

In the example above, the output shows only processes for the root user.

Change Display Units

Press the E key to choose the units you want top to use for memory values. Press lowercase e to choose the units to display within the process list view. The available options are:

  • kibibytes (KiB)
  • mebibytes (MiB)
  • gibibytes (GiB)
  • tebibytes (TiB)
  • pebibytes (PiB)
  • exbibytes (EiB - applies only for memory values)

The value that is currently in use is the first item on lines four and five.

In the following example, we have set the dashboard memory units to gibibytes, and the process list memory units to mebibytes:

Choose different display units in the top command output.

Change Output Contents

The default top output contains a lot of information and may appear confusing. Tune the contents with a few keypresses, find the information you need, or delete certain sections from the summary.

Individual CPU Core Statistics

Press 1 on the keyboard to see individual statistics for each CPU core. Press 1 repeatedly to toggle core statistics.

See CPU statistics using the top command.

CPU Usage Graph

If you want a graphical representation of CPU usage, top supports elementary ASCII graphs which denote each CPU core usage. Toggle the graphs on or off with t.

A simple graph showing CPU usage.

To change the graph display to solid block characters, press t again.

A graphical representation of CPU usage in the top command.

Press t again to remove the CPU display and task summary section.

Memory Usage Graph

The command also supports graphical display options of memory and swap memory lines. To get a visual representation of memory usage, press m. Remove the lines by pressing m again.

A graphical representation of memory usage in Linux.

Change Text Color

Emphasize the information in the output by adding color to it. Press the z key to add color to the display:

Changing the color of output text in the top command.

By default, the color changes to red. To further customize display elements and choose a different color, follow the steps below:

1. Open the color settings page by pressing Z.

Color settings page in the top command.

2. Choose which display element you want to change by pressing:

  • S. Summary Data area.
  • M. Messages and prompts.
  • H. Column headings.
  • T. Task information in the process list.

Press Enter to confirm your choice.

3. Pick the color for the selected element with one of the following keys:

  • 0. Black.
  • 1. Red.
  • 2. Green.
  • 3. Yellow.
  • 4. Blue.
  • 5. Magenta.
  • 6. Cyan.
  • 7. White.

Confirm your choice with Enter.

Now test the new settings:

Selecting a different color for the top command output.

We instructed top to print the process list in green, as in the example above.

Show Process Command Line

To see the full command line instead of the process name, press the c key. Press c again to toggle back to the process name.

Showing the process command line instead of the process name.

See Process Hierarchy

To see which processes were launched or spawned by other processes, press the V key.

How to see process hierarchy in Linux.

The tree view is useful when you want to see the child-parent process hierarchy on your system.

Show Active Tasks

To filter the output and show only active tasks, press the I key.

Filtering the top command output to show only active tasks.

The output hides the tasks that have not used any CPU resources since the last output refresh.

Limit Process Number

Press the n key to limit the process number in the output. Regardless of the number of active processes, top prompts you to enter the number of processes you want to see.

For example, we want to show only the top 5 processes:

Limiting the number of processes in the top command.

Change Process Priority

The top command allows users to change the process priority (nice value).

Follow these steps:

1. Run top and press the r key.

2. When prompted, enter the process ID and press Enter.

3. After you enter the PID, the program prompts for a new nice value. Enter a new value and press Enter.

Note: Negative nice values indicate a higher process priority.

For example, we have set a nice value of 10 to a VBoxClient process instance:

Renicing a process in Linux.

Filter Processes

The top filter allows you to use a filter expression to limit which processes to see in the list. Activate the filter option with the o key. The program prompts you to enter a filter expression.

For example, we entered the following filter:

%MEM>5.0
Filtering processes in the top command using filter expressions.

The top command now shows only the processes that used more than 5% memory. Clear the filters by pressing =.

Conclusion

The top command is a must when identifying and dealing with a problem before it affects your system. The dashboard provides plenty of system information and metrics that help you control running processes, while the various options allow you to find and resolve any possible issues quickly.

For a comprehensive list of other useful Linux commands, download our Linux commands cheat sheet.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
Fixing sub-process /usr/bin/dpkg returned an error code (1) in Ubuntu
March 5, 2024

Ddpkg is a tool used to install packages into a Debian, Ubuntu, Mint, Kali or deb based distributions. The error message “Sub-process /usr/bin/dpkg returned an error code (1)” indicates a problem with the package installer.
Read more
How to Use SFTP Commands and Options
December 1, 2021

SFTP allows users to securely transfer files using SSH. This tutorial offers an overview of SFTP commands and options, explains how they work, and provides examples.
Read more
Linux set Command & How to Use it {9 Examples}
November 23, 2021

The set command allows you to show or change the shell and environment variables in a Linux system. Follow this tutorial to learn how to use the set command and see practical examples.
Read more
AWK Command in Linux with Examples
October 28, 2021

This tutorial shows how to use the awk command in Linux to perform advanced text manipulation and processing. Learn to format a complex output to make readable files.
Read more