Linux / UNIX: Command Not Found Error and Solution

Why I am getting the error Command not found? How do I fix this problem on Linux, macOS or Unix-like systems?

One of the most common questions asked by new Linux or Unix system users. When you get the error “Command not found,” it means that Linux or UNIX searched for command everywhere it knew to look and could not find a program by that name. Another cause is that you misspelled the command name (typo) or the sysadmin administrator does not install the command on your Linux/UNIX based system. To get rid of this error, try the following suggestions:

Make sure command was not misspelled

All Linux and UNIX commands are case sensitive, and you need to type the correct spelling of the command.

Make sure command is your path

PATH is an environment shell variable that shows the different directories that your shell will see in order to find commands. You can see the current search path with the following echo command/printf command:
$ echo "$PATH"
$ printf "%s\n" "$PATH"

Sample outputs:

/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11R6/bin:/usr/local/bin:/home/vivekgite/bin

Usually, all user commands are in /bin and /usr/bin or /usr/local/bin directories. All your programs are installed in these directories. When you type the clear command, you are running /usr/bin/clear. So if it is not in your PATH, try to add directories to your search path as follows (setup Linux or UNIX search path with following bash export command):
$ export PATH=$PATH:/bin:/usr/local/bin
You can also find out of the path using which command or whereis command or type command/command as follows:
$ which ls
/bin/ls
$ which gcc
/usr/bin/gcc
$ which date
/bin/date
$ which cal
/usr/bin/cal
$ whereis gcc
/usr/bin/gcc
$ type -a gcc
gcc is /usr/bin/gcc
gcc is /bin/gcc
$ type -a time
Here is how it looks:

time is a shell keyword
time is /usr/bin/time
time is /bin/time

$ command -V time
time is a shell keyword

Typing full path

You can run a program using its full pathname. For instance, run ls as /bin/ls:

$ /bin/ls
$ /bin/date

Permission problem

Finally, sometimes you may not have permission to run the command. Remember, all Unix or Linux commands or scripts must have executable permission set. We can verify (view file permission) this using the ls command:
$ ls -l /bin/ls
$ ls -l ~/bin/toc

Outputs indicating my custom script called ~/bin/tochel has executable permission for user, group, and others:

-rwxrwxr-x 1 vivek vivek 132 Oct 19 00:31 /home/vivek/bin/toc

We can display file information and status easily by using the stat command:
$ stat /bin/ls

macOS Unix desktop or Linux desktop file manager also displays file permission. Make sure you have executable permission:

Of course, we can use the chmod command to set executable permission:
$ chmod +x filename
$ chmod +x /path/to/filename
## sysadmin level permission needed for other users ##
$ sudo chmod +x /path/to/file.sh

Run the command as the root / superuser

Some commands need privileged access. Hence run it as follows:
sudo command
sudo /path/to/command
sudo -u userName command2

See “How do I become superuser on Ubuntu Linux using su/sudo?” and “How Can I Log In As root User?” for more info.

The curious case of missing command

If a typed Unix command is not in PATH and anywhere on the system means the command or app is not installed at all. Hence, the last option is to install a missing package.

You think your command is installed on Linux or Unix/macOS desktop. Still, you or the sysadmin/vendor didn’t install the Unix command to begin with. Here is an example when I type bashtop:
$ bashtop
And system will say:

bash: bashtop: command not found

In other words, bashtop may not be installed, and we need to install the same. For instance I can install bashtop using the operating system’s package manager:
$ snap install bashtop # snaps (apps) on Linux
$ sudo apt install bashtop # Debian Linux
$ sudo dnf install bashtop # Fedora Linux
$ sudo pacman -S bashtop # Arch Linux

Now we can run the command that was missing:
$ bashtop

bashtop running on Linux desktop

Examples

Let’s assume the program you want to execute is called “cal,” and you get a “Command not found” error message from Unix or Linux system. First, type the following command to see if you get a pathname:
$ whereis cal
Sample outputs:

cal: /usr/bin/cal /usr/share/man/man1/cal.1.gz

If you do, you can invoke it using a full path name (e.g. /usr/bin/cal):
$ /usr/bin/cal
Sample outputs:

   September 2012
Su Mo Tu We Th Fr Sa
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30

Or you can add the path to your PATH variable in your shell start-up file $HOME/.cshrc (csh/tcsh) or $HOME/.bashrc (bash) file, then “source” the file:
$ vi $HOME/.bashrc
Add the path as follows:

PATH=$PATH:/home/vivek/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
export PATH

Save and close the file. Run the following source command:
$ source ~/.bashrc
$ echo "$PATH"
$ cal

Finally, you can use the following find command to search for cal command get a hit about its location.
$ find / -name cal -print
Another faster option is to use the locate command:
$ locate cal
Then, invoke it using a full path name or add it to your PATH variable in your shell startup file.

Conclusion

In short, look for the following to fix “bash: bashtop: command not found“:

  1. Make sure the shell PATH variable correctly set and verify your PATH environment variable.
  2. Verify that the file you want to execute or run exists on the Unix or Linux box.
  3. Some commands need to run as the superuser (root user). Typically we use sudo or su.
  4. Ensure the software package is installed for missing command, and you have permission to run the command.
🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
66 comments… add one
  • Sharad Kumar Sep 5, 2017 @ 13:08

    Sometimes this can be due to some comments with “#”. So removing those can help too.
    Ex :
    ###############################
    # Author : My script
    #################################

    So remove these lines.

  • Alphy Gacheru Oct 28, 2020 @ 9:26

    Thank you much!

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.