The top command in Linux lets you monitor running processes and the system resources they use. As a system administrator, it can be the most useful tool in your toolbox, especially if you know how to use it. The top utility is pre-installed on all Linux distributions. With this interactive command, you can specify how to browse the list of processes, terminate a process, print the results to a file, and much more.

This article describes several ways you can use the top command to control your system’s activities more efficiently as an administrator.

We have run the commands and procedures mentioned in this article on an Ubuntu 22.04 LTS system. We will use the Ubuntu command line, the terminal, to run our top command. You can open the terminal using either the System Dash or the Ctrl Alt T key combination.

The top Command Interface

In order to run the top command, open your Ubuntu command line, the Terminal, and enter the following:

$ top

You can open the Terminal either through the system Dash or the Ctrl Alt T shortcut.

Here is what the output of the top command looks like:

How to Use the Linux Top command linux shell ubuntu

The upper part of the output shows statistics about the processes and resource usage. The lower part displays a list of currently running processes. In order to browse through the list, you can use the arrow keys or the page up and page down keys.

If you want to quit the command, you can either hit q or use the Ctrl c shortcut that will terminate the command.

View top Command Help

The top command help will give you a list of options you can use to customize the output and the functions you can perform while you are running the top command.

In order to view help, simply hit the h key while the top command is running.

<img alt="Top command help" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-368.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="463" loading="lazy" src="data:image/svg xml,” width=”741″>

Set Interval for Refreshing the Screen

By default, the top command output is refreshed every 3 seconds. In order to change this interval, hit the d key while the top command is running. You can then enter the new time, in seconds, after which the latest output is refreshed on the screen.

<img alt="Top command refresh interval" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-369.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="279" loading="lazy" src="data:image/svg xml,” width=”824″>

Display Processes for a Specific User

If you want the top command to display processes being run by a specific user, you can use the following syntax:

$ top -u [username]

For example, the following command will display all the processes being run by root:

$ top -u root

<img alt="Limit processes shown in top command to a specific user" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-370.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="470" loading="lazy" src="data:image/svg xml,” width=”715″>

Highlight Active Processes in Top output

When you hit the z key while your top command is running, the currently active processes will be shown in color, as follows:

<img alt="Highlight Active Processes in Top output" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-371.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="584" loading="lazy" src="data:image/svg xml,” width=”861″>

View the Absolute Path of Processes

If you want to view the absolute path of the running processes, hit the c key while the top command is running. In the following output, you can see that the command now displays the path of the running processes in the Command column:

<img alt="View the Absolute Path of Processes" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-372-1024×361.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="264" loading="lazy" src="data:image/svg xml,” width=”750″>

Kill a Running Process with Top Command

A very powerful feature of the top command is that you can kill running processes through it. This is especially helpful when a process becomes unresponsive and you want to get rid of it. Hit the k key while the top command is running. A prompt will ask you about the PID you want to kill. Enter the required process ID by viewing it from the list and then hit enter. The process and the corresponding application will close almost immediately.

<img alt="Kill a Running Process with Top Command" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-373.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="503" loading="lazy" src="data:image/svg xml,” width=”861″>

Change Priority of a Process-Renice

Renice means changing the priority of a process. Hit the r key while the top command is running and then enter the PID of a process whose priority you want to change.

<img alt="Change Priority of a Process-Renice" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-374.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="290" loading="lazy" src="data:image/svg xml,” width=”841″>

Automatically Exit top Command after a Specific Number of Refreshes

As mentioned above, you can quit the top command manually through q or ctrl c. In case you want the top command to only run till a specific number of output refreshes, you can use the following command:

$ top -n [no-of-refreshes]

Example:

If you enter the following command in the Terminal, the top command will stay active till 3 refreshes and it will automatically quit afterward.

$ top -n 3

<img alt="Automatically Exit top Command after a Specific Number of Refreshes" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-375.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="29" loading="lazy" src="data:image/svg xml,” width=”367″>

Save top Command Results to a Text File

You can save the current state of your system for later use if you save the output of the top command to a text file.

Here is how you can do so:

$ top -n [no-of-intervals] -b > filename.txt

For example, the following command will save the result of one output of the top command to a file named top.txt:

$ top -n 1 -b > top.txt

<img alt="Save top Command Results to a Text File" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-376.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="42" loading="lazy" src="data:image/svg xml,” width=”493″>

The file will be saved to the current user’s home folder. If you try to open the file through any of your text editors, the contents will look like this:

<img alt="View content od saved top result" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-377.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="610" loading="lazy" src="data:image/svg xml,” width=”770″>

I have opened the file through the nano editor by entering the following command:

$ nano top.txt

Now you are well equipped with a tool that will help you in monitoring and thus managing your system resources in an optimal manner.