Manage Your Linux System Resources With These 7 Terminal Commands
Linux & macOS Terminal
Not sure what’s hogging your Linux PC’s resources? There are lots of tools for getting information about your system and managing resources. Here are some that you should know about to track processes, files, memory, and disk usage.
1 top: Processes at a Glance
One command that I’ve turned to see which processes are running on my system is top. Think of it as the Linux equivalent of Task Manager in Windows.
It’s a full-screen program that shows what’s running on my system and the resources being used, such as the CPU and RAM. The display updates every few seconds.
top shows you what processes are running, their process IDs, how much memory they’re using, and who they belong to.
2 htop: Like top, but More Colorful
htop is similar to top, but more colorful. I tend to prefer it over htop and I’m not alone. It seems to come pre-installed with a lot of Linux distros. One advantage it has over plain old top is that the interface is in color, and the bars at the top of the screen will show resource usage. It’s also easy to adjust the view, such as to sort by memory or CPU usage.
The user interface is also friendlier than plain old top. htop seems to be installed by default on a lot of distros. If it’s not, it’s one of the first things I install on a new Linux system.
On a Debian or Ubuntu system, you can use the following command to install it:
sudo apt install htop
3 free: Check Your Free Memory
free is a simple command to see how much memory you have free in your machine. When you type “free” at the command line, you’ll see the how much physical ram you’re using, as well as the swap space or virtual memory.
By default, free shows free memory in blocks when you run the command without any argument. The -f option will show it in “human readable” formats, such as gigabytes or megabytes
free -h
This makes it easier to see how much memory you’re using at a glance in the terminal.
4 vmstat: Check Virtual Memory
vmstat shows some stastics about your swap space.
Linux, like other modern operating systems, uses virtual memory to use your storage for processes that aren’t needed immediately in RAM. This allows more memory than you have in physical RAM to be used, but using the memory is slower. Processes can be “swapped out” or “swapped in” as needed by the kernel. While Windows has a swap file, most Linux distros set up a dedicated swap partition at login.
If you’re noticing sluggish performance, you can run vmstat to see how much virtual RAM is in use. You might decide to order a new RAM stick. top and htop also show physical and swap memory.
vmstat will show how much memory is used as swap, how much is free, how much is used as cache, and how much is used in buffers. Most of this information will be of interest to system programmers, though knowing how much memory you’re using is essential to troubleshoot performance issues.
5 du and df: Check Disk Space
Even though hard drives and SSDs offer plenty of space these days, sometimes it seems you never have enough. The standard Linux tools for checking disk space or du and df.
du will list the size of files in a directory. For example, to see how big files are in /usr/local/bin:
du /usr/local/bin
df shows the available space on all mounted storage devices. If you run df with a path as an argument, you’ll see the free storage of the device it’s on.
For example, to check the root directory:
df /
By default, du and df will show free space by block size. To see usage in “human-friendly” units such as MB or GB, use the -h option in both programs.
6 lsof: Check Open Files
Sometimes, you might find that the shell won’t let you log out because a file is in use. You can use the lsof command to see which files are open. It will show you a list of currently open files. It will also show a list of devices since devices are also files in the Linux system. You can close or kill an offending program, and then you can log out.
It could also be used to track unauthorized connections since network sockets will also show up in this listing. If you see a connection you don’t recognize, it could be an intruder.
The most relevant information is the name of the command and the full pathname of the open file. These are under the “COMMAND” and “NAME” fields in the table that’s printed into the terminal.
7 uptime: How Long Has Your Machine Been Up?
The uptime command shows how long your machine has been running. It will show the current time, how long the system has been up since it was last booted, the number of users currently logged in, and the load averages for the past minute, 15 minutes, and 30 minutes.
This is a useful tool for servers, but lots of Linux users love to brag about their uptime as well. It’s a simple command with fewer options. Even the manual page is short. You can use the -s option to show how when the system was booted by itself
uptime -s