Skip to main content

How to fix a "Command not found" error in Linux

Learn five ways to deal with a "Command not found" error, Linux users.

When you're trying to run a command (with or without sudo) and get an error message that reads "Command not found," this means the script or file you're trying to execute doesn't exist in the location specified by your PATH variable. What is this variable, and how can you run commands that it can't find?

[ Related reading: Understand file paths and how to use them in Linux

Understanding environment variables

In computing, a variable is a placeholder for a value that can change. You use variables every day in normal speech, although you don't think of them as such. When you say "my laptop," you're using "laptop" as a generic variable or placeholder for the computer you're carrying, regardless of whether it happens to be a Lenovo, Mac, or a Raspberry Pi in a fancy case.

Environment variables are special variables that contain information about your login session. Many of these variables are set by default during installation or user creation. They're stored for the system shell, applications, and scripts to use when executing commands.

There are global, or system-defined, variables and local, or user-defined, variables.

Global variables

Global variables come predefined in your login shell, but they aren't immutable and can be modified or deleted according to your preferences. You can use the printenv or env commands to display the environment variables on your system:

$ env 
SHELL=/bin/bash 
SESSION_MANAGER=local/kiwi.homelinux.local:@/tmp/.ICE-unix/1906,unix/kiwi.homelinux.local:/tmp/.ICE-unix/19
06 
WINDOWID=153092103 
COLORTERM=truecolor 
XDG_CONFIG_DIRS=/home/tux/.config/kdedefaults:/etc/xdg:/etc/kde/xdg 
LESS=-XR 
XDG_SESSION_PATH=/org/freedesktop/DisplayManager/Session1 
HISTCONTROL=:ignorespace:ignoredups:ignorespace:ignoredups 
PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig:/usr/local/share/pkgconfig:/usr/lib64/pkgconfig:/usr/share/pkgconfig 
[...]

The env command prints out all global environment variables. Variables are case sensitive, and all Linux distributions use uppercase for environment variable names by default.

[ Keep your favorite Git commands, aliases, and tips close at hand. Download the Git cheat sheet. ]

Local variables

A local variable exists only within a specific shell. Therefore, when you define a local variable, it's only available in your current shell. It doesn't propagate or persist to a new shell session unless you export it as a global variable.

Local variables are often defined in lowercase to avoid overwriting a global variable with the same name.

The PATH environment variable

The PATH global environment variable lists the directories your system searches for valid, executable commands. By default, it contains standard directories that normally store executables like /usr/bin, /usr/local/bin, and so on.

When you type in a command, such as grep or vim, your system searches through all directories listed in your PATH variable, in the order that they're listed, until it finds an executable file by the same name. Should it fail to find one, it issues the "Command not found" error.

$ printenv PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/tux/.local/bin:/home/tux/bin

$ env $PATH
env: /usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/tux/.local/bin:/home/tux/bin

5 ways to fix "Command not found" errors

There are several ways to fix this problem. Here are five of them.

1. Include the path

Not everything you want to execute needs to be in your path. You can execute files directly by specifying the path to the file you want to run. By identifying the file's location, you circumvent the need for your system to search your path at all.

For example, suppose you have a script called hello that you want to run. It's located in your home directory, and you have already marked it as executable with chmod +x:

$ ~/hello
hello world

By telling your system the file's location, the PATH variable is never involved, and the file runs as expected.

2. Add a new path

Alternately, you can add a new directory to your PATH. Add your executable files to that directory, and then you can run them without manually providing a path:

$ cp ~/hello ~/.local/bin
$ export PATH=$PATH:$HOME/.local/bin
$ printenv PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/tux/.local/bin

You may want to add the new PATH environment variables to your login shell by including them in your .bashrc file as new settings.

[ Download now: A sysadmin's guide to Bash scripting. ]

3. Copy a file to an existing path location

If you want to execute your binary file or script, copy it to any of the directory paths already listed in the PATH environment variable:

$ sudo cp ~/hello /usr/local/bin/
$ hello
hello world

4. Tell Bash where to look

Probably the simplest option, especially for one-off scripts or applications, is to tell Bash not to consider the PATH but rather to "look here." Do this by placing a dot and a slash in front of the command, script, or application name. For the hello script, it looks like this:

$ sudo ./hello
hello world

No permanent changes are made to the system. This might be handy if you're writing a script and want to test it before copying or moving it to its normal storage location (presumably along the PATH).

5. Install a package

Sometimes when you try to use a command and Bash displays the "Command not found" error, it might be because the program is not installed on your system. Correct this by installing a software package containing the command. For example, if you don't have Nmap installed, then the nmap command fails when you type it into a terminal:

$ nmap
nmap: command not found
$ sudo dnf install --assumeyes --quiet nmap
$ nmap
Nmap 7.92 ( https://nmap.org ) 
Usage: nmap [Scan Type(s)] [Options] {target specification}
[...]

[ How well do you know Linux? Take a quiz and get a badge. ]

Stick to the path

The PATH variable is a powerful tool you can use to customize how your system responds to commands, so take some time to get comfortable with it. It's frequently used when running commands to find the command executable.

In this tutorial, you learned five ways to fix a "Command not found" error in your terminal—three of which rely on the PATH variable. Now that you know what variables are and how command executables are found, you won't be so mystified when the "Command not found" error appears on your screen.

[ Learning path: Getting started with Red Hat OpenShift Service on AWS (ROSA)

Topics:   Linux   Troubleshooting  
Author’s photo

Shehu Awwal

Shehu Awwal is a hacker who is passionate about InfoSec, Linux, cloud, containers, virtualization, distributed systems, architectures, and new trends. More about me

Try Red Hat Enterprise Linux

Download it at no charge from the Red Hat Developer program.