How to Use the ‘htop’ Command in Linux

Navigate Linux processes, memory, and CPU with the interactive 'htop' command.

The htop command, an acronym for Hisham’s top, is a powerful tool that provides a dynamic real-time view of a running system. Unlike the traditional top command, htop offers a more interactive and user-friendly interface, displaying a complete overview of all processes, CPU usage, memory, and other essential system statistics.

It’s particularly handy for those who need to monitor system performance, manage processes, or troubleshoot issues. Similar to the top command, htop can be used alongside other commands like ps and vmstat to gain a comprehensive understanding of system behavior.

How to Install the htop Command

htop is often not included by default in many Linux distributions, so you may need to install it. Here’s how you can install and uninstall htop on some common Linux distributions:

For Debian-based systems (like Ubuntu):

To install htop, open a terminal and run:

sudo apt-get update
sudo apt-get install htop

To uninstall htop, you can use:

sudo apt-get remove htop

For Red Hat-based systems (like Fedora):

To install htop, use:

sudo dnf install htop

To uninstall, use:

sudo dnf remove htop

For SUSE-based systems:

To install htop, use:

sudo zypper install htop

To uninstall, use:

sudo zypper remove htop

For Arch Linux:

To install htop, use:

sudo pacman -S htop

To uninstall, use:

sudo pacman -R htop

How to Use htop

1. Viewing System Processes and Resources

Syntax: htop

Explanation: Displays an interactive overview of system processes and resource usage.

Example: htop

Output:

1  [|||||||                    16.7%]     Tasks: 34, 60 thr; 2 running
2  [||||||||                   20.0%]     Load average: 0.07 0.02 0.00 
Mem[|||||||||||||||       512M/1000M]     Uptime: 02:23:45
Swp[|||                      50M/100M]

This output shows the CPU usage on two cores, memory usage, swap usage, number of tasks, load averages, and system uptime. The bars provide a visual representation of the resource consumption.

2. Filtering Processes by User

Syntax: htop -u USERNAME

Explanation: Displays only the processes owned by a specific user.

Example: htop -u john

Output:

PID USER      PRI  NI  VIRT   RES   SHR S CPU% MEM%   TIME+  Command
1234 john       20   0 1024M  100M 9000 S  5.0 10.0  0:00.91 /usr/bin/app

This output shows the processes owned by the user john, including details like PID, priority, virtual memory, resident memory, shared memory, CPU, and memory percentage.

3. Sorting Processes by Memory Usage

Syntax: htop -s PERCENT_MEM

Explanation: Displays processes sorted by memory usage.

Example: htop -s PERCENT_MEM

Output:

PID USER      PRI  NI  VIRT   RES   SHR S CPU% MEM%   TIME+  Command
5678 alice      20   0 2048M  200M 9000 S 10.0 20.0  0:01.23 /usr/bin/heavyapp
1234 john       20   0 1024M  100M 9000 S  5.0 10.0  0:00.91 /usr/bin/app

This output shows the processes sorted by memory usage, with the process consuming the most memory at the top. It includes details like PID, user, priority, virtual and resident memory, CPU, and memory percentage.

4. Showing Only Processes with a Specific String

Syntax: htop -p PID[,PID...]

Explanation: Displays only the processes with the specified Process IDs (PIDs).

Example: htop -p 1234,5678

Output:

PID USER      PRI  NI  VIRT   RES   SHR S CPU% MEM%   TIME+  Command
1234 john       20   0 1024M  100M 9000 S  5.0 10.0  0:00.91 /usr/bin/app
5678 alice      20   0 2048M  200M 9000 S 10.0 20.0  0:01.23 /usr/bin/heavyapp

This output shows only the processes with the specified PIDs, 1234 and 5678, along with details like user, priority, memory usage, CPU, and memory percentage.

5. Displaying Processes in Tree View

Syntax: htop -t

Explanation: Displays processes in a tree view, showing parent-child relationships.

Example: htop -t

Output:

PID USER      Command
1   root      /sbin/init
 `-1234 john   \_ /usr/bin/app
    `-5678 alice  \_ /usr/bin/heavyapp

This output shows the processes in a tree view, illustrating how processes are related to each other. The parent process is listed first, with child processes indented beneath it.

6. Showing Absolute Path of Command

Syntax: htop -c

Explanation: Displays the absolute path of the command being run for each process.

Example: htop -c

Output:

PID USER      PRI  NI  VIRT   RES   SHR S CPU% MEM%   TIME+  Command
1234 john       20   0 1024M  100M 9000 S  5.0 10.0  0:00.91 /usr/bin/app
5678 alice      20   0 2048M  200M 9000 S 10.0 20.0  0:01.23 /opt/custom/heavyapp

This output shows the full absolute path of the command being run for each process, providing more detailed information about the location of the executable files.

More Linux commands:
Directory Operations rmdir · cd · pwd · exa · ls
File Operations cat · cp · dd · less · touch · ln · rename · more · head
File System Operations chown · mkfs · locate
Networking ping · curl · wget · iptables · mtr
Search and Text Processing find · grep · sed · whatis · ripgrep · fd · tldr
System Information and Management env · history · top · who · htop · glances · lsof
User and Session Management screen · su · sudo · open
WebsiteFacebookTwitterInstagramPinterestLinkedInGoogle+YoutubeRedditDribbbleBehanceGithubCodePenWhatsappEmail