How to fix bash:command not found error in Linux/Unix

In my last article I had shared the instructions which can be used to check the path of any command on your Linux machine so that you do not end up with this error

Most of you must have been facing this issue. Actually this can not be considered as as error until and unless the package file for the command is missing or has corrupted due to some reason because in that case you might have to re-install the package.

Well in this post I will help you out and prepare you for both the possibilities.

Suppose you are getting an error

# useradd deepak
-bash: useradd: command not found

This happens generally when the path variable for that command is not defined. So in that case you will have to manually define the path variable. This error is command independent, so any time you face this issue kindly check for the location of the command and add the complete path to PATH variable.
There are few methods which you can use to find the location of the missing command

Method 1
Look out for the missing command using

# whereis useradd
useradd: /usr/sbin/useradd /usr/share/man/man8/useradd.8.gz

Method 2
You can also use the below command but this might not work all the time because ultimately this command uses .bash_profile and .bashrc for all the saved PATH variable of all the executable file

# which useradd
/usr/sbin/useradd

Method 3
You can use find command ultimately to look out for the missing command or file (As all the command are executable files)

# find / -name useradd -type f
/usr/sbin/useradd
So now once you got the location of the command, add the path to the PATH variable

# export PATH=/usr/sbin:$PATH
But again running this export command will be session dependent so once you change the session you will again have to export the new PATH variable


To make permanent changes in your machine, add this command to your .bash_profile

# vi /root/.bash_profile
export PATH=/usr/sbin:$PATH So now every time your system reboots this command will be executed and the PATH variable will be updated.

Permanent Fix:

To fix this issue permanently so that this issue does not comes up again. You can add the default location of all the directories in the PATH variable which contains all the executable files in Red Hat Linux in your .bash_profile file.

You should also read the difference between .bash_profile and .bashrc, so that you can use the file wisely?

export PATH=$PATH:/usr/bin:/usr/sbin:/usr/local/bin:/usrlocal/sbin

Question:
Now in case you do not find the useradd file in your machine or even after adding the PATH variable you are getting some error then there is a possibility that related rpm package has corrupted and you need to re-install that package.

Now how are you going to find the package name responsible for "useradd"

Solution:
You can use yum to check the rpm file responsible for this executable file.


NOTE: The following commands are tested on RedHat and CentOS
First find the full path of the executable file using the below command or any of the command shown above
# which useradd
/usr/sbin/useradd
Solution 1
Then run the below command
$ rpm -qf /usr/sbin/useradd
shadow-utils-4.0.17-15.el5
As you see the package responsible for useradd binary file is shadow-utils


Solution 2

# yum whatprovides  */useradd
2:
shadow-utils-4.1.4.2-13.el6.i686 : Utilities for managing accounts and shadow
                                  : password files
Repo        : base
Matched from:
Filename    : /etc/default/useradd
Filename    : /usr/sbin/useradd

As you can see shadow-utils-4.1.4.2-13.el6.i686 is the rpm file which can help you re-install "useradd"

Now you can manually install this rpm file

# yum -y install shadow-utils