Fixing “bash: command not found” error
Method 1: Double check the command name (no, seriously)
It is human to make mistakes, specially while typing. It is possible that the command you entered has a typo (spelling mistake).
You should specially pay attention to:
- The correct command name
- The spaces between the command and its options
- The use of 1 (numeral one), I (capital i) and l (lowercase L)
- Use of uppercase and lowercase characters
Take a look at the example below, where I have misspelled the common ls command.
So, make double sure what you are typing.
Method 2: Ensure that the command is installed on your system
This is another common reason behind the command not found error. You cannot run a command if it is not installed already.
While your Linux distribution comes with a huge number of commands installed by default, it is not possible to pre-install all the command line tools in a system. If the command you are trying to run is not a popular, common command, you’ll have to install it first.
You can use your distribution’s package manager to install it.
You may have to install the missing command
In some cases, popular commands may get discontinued and you may not even install it anymore. You’ll have to find an alternative command to achieve the result.
Take the example of ifconfig command. This deprecated command was used for getting Ip address and other network interface information. Older tutorials on the web still mention using this command but you cannot use it anymore in newer Linux versions. It has been replaced by the ip tool.
Some popular commands get discontinued over the time
Occasionally, your system won’t find even the extremely common commands. This is often the case when you are running a Linux distribution in Docker containers. To cut down on the size of the operating system image, the containers often do not include even the most common Linux commands.
This is why Docker user stumble across things like ping command not found error etc.
Docker containers often have only a few commands installed
So, the solution is to either install the missing command or find a tool that could do the same thing you were trying to do with the missing command.
Method 3: Check if it is an executable script with correct path
This is a common mistake Linux rookies make while running a shell script.
Even if you are in the same directory and try to run an executable script just by its name, it will show an error.
You need to either specify the shell interpreter explicitly or its absolute path.
If you are in some other directory and try to execute the shell script without giving the correct path to the file, it will complain about not finding the file.
Adding it to the PATH
In some cases, you download the entire software in a tar file, extract it and find an executable file along with other program files. To run the program, you need to run the executable file.
But for that, you need to be in the same directory or specify the entire path to the executable file. This is tiresome.
Here, you can use the PATH variable. This variable has a collection of directories and these directories have the binary (executable) files of various Linux commands. When you run a command, your Linux system checks the mentioned directories in the PATH variable to look for the executable file of that command.
You can check the location of the binary of a command by using the command:
If you want to run an executable file or script from anywhere on the system, you need to add the location of the file to this PATH variable.
The PATH variable then needs to be added to the rc file of the shell so that the changes made to PATH variable is permanent.
You get the gist here. It is important that your Linux system has the knowledge about the location of the executable script. Either you give the path while running it or you add its location to the PATH variable.
Verify your PATH environment variable
Most of the time, you will run into the “bash : command not found” after changing your PATH environment in order to add new entries.
First, verify that the path you searched for before is listed in your PATH environment variable.
As you can see here, the “/bin” directory is not listed in my PATH environment variable.
By default, the PATH is defined in the “/etc/environment” file for all the users on the system.
If your PATH environment variable is different from the one defined in the environment file, it is because you have overriden the PATH.
Now that you have two choices : either you know where you exported the PATH variable or you don’t.
Fixing your profile scripts : bashrc, bash_profile
In most of the cases, you modified the .bashrc or the .bash_profile file in order to add your PATH override.
To search where you exported your PATH, run the following command
This command returns the file where the PATH was exported as well as the line number.
Edit this file and add the path from the first section to the export statement.
Save your file and exit the nano editor.
For the changes to be applied, you will have to source your current bash terminal.
This will ensure that the .bashrc file is executed again in the current shell terminal.
Now, you can try to execute the command you failed to execute before.
You fixed the “bash : command not found” error on Linux!
Reset the PATH environment variable properly
Even if you solve your issue, you will have to define your PATH environment variable properly if you don’t want to modify your bashrc file all the time.
First, have a look at the PATH variable defined in the “/etc/environment” file.
In order to reset your PATH environment variable on your environment, export the PATH defined in the environment file.
Now, modify your .bashrc file but use the $PATH syntax in order to append your paths to the existing PATH variable.
Exit the file and source your bashrc file for the changes to be applied.
You have successfully resetted your PATH environment variable, you should not get the “bash : command not found” error anymore.
Bash & PATH concepts
Before starting out with the solution, it is important to have a few concepts about what the PATH environment variable is and how it is related to the commands you run.
PATH is an environment variable that lists the different directories that your bash terminal will visit in order to find utilities on your system.
To have a look at your PATH environment variable, simply use the “echo” command with the PATH variable.
As you can see, PATH is defined by a list of different system paths delimited by colons.
They are the different paths visited by my interpreter in order to run commands.
If I were to remove an entry from the PATH, or remove the PATH all together, you would not be able to run commands in the bash without specifying the entire path to the binary.
It is an important point to understand because not being able to run a command does not mean that your binary was deleted on the system.
Now that you understand how environment variables are related to your bash interpreter, let’s see how you can solve your error.
Как разрешить ошибку «bash: wget: command not found»
Для того, чтобы решить ошибку «bash: wget: command not found», вам необходимо установить утилиту wget на сервере.
В Debian и Ubuntu:
[email protected]:~# apt-get install wget
Пример журнала установки:
[email protected]:~# apt-get install wget Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: wget 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 298 kB of archives. After this operation, 901 kB of additional disk space will be used. Get:1 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu xenial-updates/main amd64 wget amd64 1.17.1-1ubuntu1.2 Fetched 298 kB in 0s (401 kB/s) Selecting previously unselected package wget. (Reading database ... 51222 files and directories currently installed.) Preparing to unpack .../wget_1.17.1-1ubuntu1.2_amd64.deb ... Unpacking wget (1.17.1-1ubuntu1.2) ... Processing triggers for install-info (6.1.0.dfsg.1-5) ... Processing triggers for man-db (2.7.5-1) ... Setting up wget (1.17.1-1ubuntu1.2) ...
На RHEL и Centos.
# yum install wget
Пример журнала установки:
# yum install wget Loaded plugins: amazon-id, rhui-lb, search-disabled-repos Resolving Dependencies --> Running transaction check ---> Package wget.x86_64 0:1.14-13.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ======================================================================================================================================================================== Package Arch Version Repository Size ======================================================================================================================================================================== Installing: wget x86_64 1.14-13.el7 rhui-REGION-rhel-server-releases 546 k Transaction Summary ======================================================================================================================================================================== Install 1 Package Total download size: 546 k Installed size: 2.0 M Is this ok [y/d/N]: y Downloading packages: wget-1.14-13.el7.x86_64.rpm | 546 kB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : wget-1.14-13.el7.x86_64 1/1 Verifying : wget-1.14-13.el7.x86_64 1/1 Installed: wget.x86_64 0:1.14-13.el7 Complete! #
Как использовать wget для загрузки файлов через Интернет.
wget http://url/file or wget ftp://url/file
Примеры журнала загрузки:
# wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm --2017-05-06 11:21:34-- http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm Resolving packages.sw.be (packages.sw.be)... failed: Name or service not known. wget: unable to resolve host address ‘packages.sw.be’ # wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm --2017-05-06 11:21:34-- http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm Resolving dl.fedoraproject.org (dl.fedoraproject.org)... 18.104.22.168, 22.214.171.124, 126.96.36.199 Connecting to dl.fedoraproject.org (dl.fedoraproject.org)|188.8.131.52|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 14704 (14K) [application/x-rpm] Saving to: ‘epel-release-7-9.noarch.rpm’ 100% 14,704 57.8KB/s in 0.2s 2017-05-06 11:21:34 (57.8 KB/s) - ‘epel-release-7-9.noarch.rpm’ saved [14704/14704]
В приведенном выше примере мы загрузили файл «rpmforge-релиз-0.5.2-2.el6.rf.x86_64.rpm» с адреса URL «packages.sw.be/rpmforge-release/rpmforge-release-0.5.2- 2.el6.rf.x86_64.rpm»с использованием протокола HTTP.