Command not found on Linux

How to Fix bash Command Not Found on Linux

It is crucial to learn how to fix “Command not found” error on Linux machines. At times, the users gets this error because the commands that users run do not exist in the Linux system. Hence, in this article, we will look at why this error occurs and what you can do to fix it. So, let’s get started. 

The Command Not Found Error – Quick Intro

The “Command not found” error usually occurs in a Linux system when the shell has searched for the command everywhere and could not find any program by that name. Or, you have misspelled the command on the terminal. Alternatively, if a system administrator manages your machine, then they have not installed the particular program on your Linux machine. 

How to Fix Bash Command Not Found on Linux

There are multiple ways to fix the “Command not found” error. We will discuss six different ways in which you can troubleshoot this. 

1. Check the Program’s Spelling

The first thing to check is the spelling of the command that you wrote in the terminal. If the command is misspelled, then BASH won’t recognize it and will give the error. For instance, if you misspell the nano command and write naon, then you should get this error:

misspelled command

Also, the Nano command won’t work either, as the shell commands are case-sensitive. 

Output:

Fix command not found error

So make sure to take care of the correct spelling and write all commands in lowercase. In short, pay attention to the following:

  • Correct command name
  • Spaces between command and its arguments
  • Use of options and flags with the command
  • Uppercase and lowercase letters in the program

2. Check the PATH Variable to Fix the Command Not Found Error

The PATH variable is a powerful system variable that manages the command control. You can modify it to customize how your Linux machine reacts to the commands. Before starting with this step, it is crucial to know about the PATH variable. PATH is a system variable responsible for listing different directories that your bash terminal will use to run the system utilities. 

Run the PATH variable in the terminal, and you should see the location of different directories. Specifically, type:

echo $PATH

As you can see from the output, PATH consists of different directories separated by the colon. The BASH interpreter visits these locations to run the different commands. If you remove any directory from the PATH, you won’t be able to run that program without indicating its location first. 

If the command you are trying to run gives the “Command not found” error, add its path in the PATH variable. Type:

sudo export PATH=$PATH:/bin:/usr/local/bin

Then print the $PATH to confirm that the location to the command has been added. Specifically, input:

echo $PATH

By default, all the commands are located in the /bin and /usr/bin or /usr/local/bin directories. However, if you are unaware of the path of the program you want to add to the $PATH, use the which command. The syntax of the command looks something like this:

which <command name>

For example, to check the path to the docker module, type:

which docker

Output:

check command using which

3. Check the Command Permission

At times, the commands don’t have permission to execute. You can verify this if you get the “Command not found” error. To check, execute:

ls -l /bin/ls

To assign the executable permissions, use the chmod command. The syntax of the command is:

chmod [OPTION]... MODE[,MODE]... FILE...

For example:

sudo chmod +x /path/to/filename

For the specific user or group, type:

sudo chmod u+x /path/to/filename
sudo chmod g+x /path/to/filename

4. Run the Commands as the Root/Super User to Fix the Command Not Found Error on Linux

If setting the $PATH and changing the command’s permission still produces the error “Command not found”, then try running the command as the root user or a super user. For this step, add the sudo keyword before running the command. For example, to run the docker container in the terminal, type:

sudo docker
sudo -u sidrah docker

5. Check the Command Alias

At times, the command you are trying to run gives the “Command not found” error because you are trying to run a well-known alias of the command instead of its actual name. In Linux, the users have the flexibility to create short names for longer commands. For instance, the alias for the vim command is vi. 

Let’s suppose you try to invoke the vim editor using the vi command, and you don’t have any alias set, so you should get the “Command not found” error. Therefore, ensure that you are using the correct command name. Alternatively, you can set the alias.

First, open the .bashrc profile file:

vim ~/.bashrc

Then enter your alias in the file:

alias ll="ls -alF"

Output:

fix command not found

Save and exit the text editor.

6. Install the Package

The last option is to install the package if you get the error “Command not found”. After trying all the steps, if the error persists, then it means that the command does not exist. Hence, the last option is to install the missing package. 

For instance, check for the bashtop command as shown below:

bashtop

Output:

fix command not found error

Now, check for its path. Type the command given below to get its pathname:

whereis bashtop

Output:

fix command not found on linux

If the path does not appear, then the last step is to search for it using the find command. For instance, type:

find / -name bashtop -print

Alternatively, you can use the locate command for the same purpose. Input:

locate bashtop

If the error appears, you need to install it. Use the operating system’s package manager to install any command as shown below:

# for apps on Linux
snap install bashtop 
# for Debian Linux
sudo apt install bashtop 
# for Fedora version
sudo dnf install bashtop 
# for Arch Linux 
sudo pacman -S bashtop 

# for apps on Linux

snap install bashtop 

# for Debian Linux

sudo apt install bashtop 

# for Fedora version

sudo dnf install bashtop 

# for Arch Linux 

sudo pacman -S bashtop 

Wait for the installation to complete. Lastly, run the command again in the terminal:

bashtop

In conclusion, there are several ways to fix the “Command not found” error on Linux. It is crucial to figure out the root problem first, instead of resorting to installing the command directly. The pointers discussed in this guide should help you in troubleshooting this error. 

Were you able to fix the error? Let us know in the comments. Also, learn how to fix the no route to host and apt command not found error at Distroid. 

If this guide helped you, please share it.

Leave a Reply
Related Posts