Linux / UNIX: Command Not Found Error and Solution

Why I am getting the error Command not found? How do I fix this problem?

A common question asked by new Linux or UNIX 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 you misspelled the command name (typo) or administrator does not at all 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 correct spelling of command.

Make sure command is your path

You can see current search path with following command:

$ echo $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 path of any command with which or whereis commands:

$ which ls

/bin/ls

$ which gcc

/usr/bin/gcc

$ which date

/bin/date

$ which cal

/usr/bin/cal

$ whereis gcc

/usr/bin/gcc

You can run a program using its full pathname as follows:

$ /bin/ls
$ /bin/date

Finally, sometime you may not have permission to run the command.

Example

Let’s assume the program you want to execute is called “cal” and you get a “Command not found” message from Unix or Linux. First, type the following command to see if you get a path name:
$ 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 command:
$ source ~/.bashrc
$ echo $PATH
$ cal

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

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.