How to Fix the “command not found” Error on Linux
If you’re a Linux user, you’ve most likely encountered the “command not found” error on the Linux terminal.
Often when you come across this error, you’ll get a suggestion to install a program, however, there are several potential causes for the “command not found” error.
Understanding Environment Variables and Case Sensitivity
Before we go about fixing common causes of the error, it is important to appreciate a few critical properties of Linux commands.
Unlike Windows, all commands, filenames, and directories on Linux and Unix systems are case-sensitive. This means that all commands and their corresponding arguments or options should appear exactly as they are meant to. Of course, most commands are lowercase.
Environment variables play an important role in Linux. They contain valuable information about your login session and other important details.
When you run a command on Linux, your system will search in your current variables, specifically the PATH variable, to find the command or program that you want to run. If the command is not found, you’ll get an error that it’s not found.
With this understanding, here’s how you can troubleshoot the “command not found” error on Linux.
1. Check for Syntax Errors
You should run all Linux commands exactly as they appear case-wise. For example, the shell treats “ls” and “LS” as different commands on Linux.
Also, if your command contains arguments, make sure you’re using the correct case. The ssh command for port forwarding takes the format:
ssh -fN -L 80:localhost:8080
The “L” and the “N” arguments, in this case, must be uppercase, otherwise, you’ll get an error.
In some cases, the options or arguments may require a preceding hyphen (-) or double hyphen (–), and some arguments may need to be enclosed in quotes or brackets.
Make sure you are using the correct command. Double-check the spelling of the command you are trying to use and ensure that you are using the correct case.
2. Include the Path to Your Executable
Scripts play a vital role in Linux as they allow you to automate mundane tasks. If you write your own Bash program or use some third-party programs, you’ll need to specify the entire path to the program to execute it successfully.
When you download the Open Virtualization Format Tool, for example, you can install it to any directory of your liking. Now, when you try to run the ovftool command from a directory other than the one it was installed in, you’ll get the “command not found” error.
To run the command properly, you need to specify the entire path to the program executable. If, for instance, you’ve installed ovftool in the /opt/ovf directory, then to run ovftool, you’ll have to use the following command:
Also, pay attention to the slashes. Unlike Windows, which uses backward slashes, Linux uses forward slashes. Use the Tab auto-completion feature of your terminal to avoid typos in directory names.
3. Use Environment Variables
If you find that specifying an entire directory to a command is tiresome, consider adding the program executable to your PATH environment variable.
To add a program executable to PATH, simply run a command in the following format:
PATH = $PATH:/opt/path/to/your/program
For example, to add the Microsoft .NET tools program to your PATH, run the command:
Environment variables are written to the ~/.bashrc file on Bash or the ~/.zshrc file if you’re using the Z shell.
To update and apply the changes you’ve made to the ~/.bashrc file without logging out, run the source command as follows:
You can check if your program path is successfully added to the PATH variable using the command:
4. Make Sure That the Program Is Installed
Sometimes, the cause of the “command not found” error could simply be because the program is not even installed in the first place.
If the command you are trying to run is not a built-in shell command, it may be that the program is not installed on your system. In this case, you will need to install the program or the package that contains it.
Depending on your distro, use the default package manager to install the required software. On Debian-based distros, use the apt command, and use DNF or YUM on RHEL-based distros.
5. Use the Correct Privileges
If you are very certain that the program or command you’re trying to run is on your system, and you are using the correct syntax then it could be an issue with the permissions.
Similar to Windows, you’ll need elevated privileges to execute certain programs on Linux. If that’s the case, make sure that you’re using sudo or running the program as the root user.
Make Use of Environment Variables on Linux
Ensure you use the correct syntax when executing Linux commands and consider adding the programs that you often run to your environment variables.
Environment variables play such an important role in Linux that understanding them is key for your day-to-day use of the operating system.