“Command not found” is an error message that is displayed in the terminal when the shell is unable to locate an executable file or command that you have entered.
There are several reasons why this error can occur:
- You may have mistyped the command name.
- The command is not installed on your system.
- The command is installed, but it is not in a directory listed in your PATH environment variable.
- The command requires superuser or root privileges to run and you are not currently running the shell as superuser or root.
In this article, we will deep dive into this issue and explore a few ways to fix the “command not found” error.
Table of Contents
Check the spelling of the command
Make sure you have spelled the command correctly.
One of the most common causes of the “command not found” error is simply a typo or a misspelling of the command. It is important to double check the spelling of the command before attempting to run it.
Carefully review the spelling of the command, paying attention to the capitalization.
Many Linux shells have an autocomplete feature. You can press the TAB key after typing a portion of the command to have the shell automatically complete the command or show you a list of possible commands that starts with the letters you typed.
If you run the command before, you can use the “history” command to show a list of previously run commands. The history command is very useful when you want to quickly recall and execute a command that you previously ran.
history | grep 'pattern'
This command will show the command history that contains the ‘pattern’ in the command.
The history command is a built-in command of the shell and it is available on most Linux distributions.
You can use it in different shells like bash, zsh, csh, etc. it can be very useful when you need to recall and repeat a command that you previously ran, especially when you are working on repetitive tasks or when you need to troubleshoot an issue.
Verify that the command is installed
Another most common causes of the “command not found” error is that the command is not installed.
Use the command “which [command]” or “whereis [command]” to check if the command is installed on your system.
The “which” command shows the path of the executable associated with the command. For example, if you type “which ls”, it will show the path of the “ls” command.
The “whereis” command shows the location of the binary, source, and manual page files for a command. For example, if you type “whereis ls”, it will show the path of the binary, source and man page of the command “ls”.
If the command is not installed, “which” command will return “no <command> in” and “whereis” command will return “<command>:” with nothing after the colon.
In this case, we need to install the package.
The command to install a package in Linux depends on the distribution you are using. The most common package managers are apt (Debian/Ubuntu) and yum (Red Hat/CentOS).
To install a package using apt, the command is:
sudo apt install package_name
To install a package using yum, the command is:
sudo yum install package_name
If you are not sure which package you need to install, we can use yum provides command.
For example, if you want to check which package the ls command is in, you can run the following command:
yum provides $(which ls)
Or you can use
yum provides '*/ls'
This command will show you a list of packages that contain the file ls. The output will include the package name, version and repository the package belongs to.
Additionally, you can use yum whatprovides command to check the package that provides a specific command or file, for example:
yum whatprovides '*/ls'
You can use this command to get more detailed information about a package that provides a specific command or file.
Add the path of the command to your PATH environment variable
If the command is installed, but not in a directory listed in your PATH environment variable, you can add the path to the command to your PATH.
The PATH is an environment variable in Linux and other Unix-like operating systems that tells the shell where to look for executable files or commands.
When you enter a command in the terminal, the shell looks for that command in a list of directories specified in the PATH variable.
The directories in the PATH variable are separated by a colon (:) and are searched in the order they appear.
For example, if the PATH variable is set to:
When you enter a command, the shell will first look for it in the /usr/local/bin directory, then in the /usr/bin directory, and finally in the /bin directory.
If the command is not found in any of these directories, the shell will return a “command not found” error.
You can view the current value of the PATH variable by running the command:
You can add new directories to the PATH variable by editing the .bashrc or .bash_profile file in your home directory and adding the directory to the PATH variable.
After you’ve done this, you should be able to run the command from anywhere in the terminal.
Please note that the above steps are for bash shell, if you are using different shell like zsh, csh, etc, the steps and file name will be different.
Try using a superuser or root account
Some commands require superuser or root privileges to run, try running the command as superuser or root.
These commands are typically used for system administration tasks such as installing software, managing system services, or modifying system configuration.
You can run a command as superuser or root by prefixing the command with the sudo command. For example, to run the apt-get update command as superuser, you would use the following command:
sudo apt-get update
You will be prompted for your password before the command is executed.
Alternatively, you can switch to the root account by running the su command and then entering the root password.
Please be aware that running commands as superuser or root can be dangerous, as it allows you to make changes to the system that can potentially break it. You should use these privileges only when necessary and be cautious when running commands as superuser or root.
It is a best practice to use sudo command instead of switching to root account, as sudo allows you to run a command with root privileges without logging in as the root user, and it also logs the commands run and by who, which is useful for auditing purposes.
The “command not found” error is a common problem that can occur when using the terminal in Linux.
To resolve the “command not found” error, you can try verifying that the command is installed on the system, adding the path of the command to the PATH environment variable, checking for typos in the command name, running the command as superuser or root.
It’s important to keep in mind that different Linux distributions may have different command names, so always check the documentation of your distribution for the correct command name.