Typically the “which” command is installed by default though depending on what Linux distribution and what packages you decided to install there is a possibility the command is not installed. Installing which is easy on any distribution as the package is literally just called “which”. Below is a brief explanation of the error you could get if which is not installed and information on how to install the which package using the yum package manager on CentOS.
Error: -bash: which: command not found
Attempt To Use Which When The Package Is Not Installed:
- [root@dev ~]# file `which ruby`
- -bash: which: command not found
- Usage: file [-bcikLhnNsvz] [-f namefile] [-F separator] [-m magicfiles] file...
- file -C -m magicfiles
- Try `file --help' for more information.
As you can see above when attempting to use the which command an error is displayed. I first thought that which had to be installed but maybe not in my path however that was not the case either. I then verified that which was not installed using the yum package manager as shown below.
Verify which Is Not Installed Using Yum Package Manager:
- [root@dev ~]# yum list *WHICH*
- Loading "fastestmirror" plugin
- Loading mirror speeds from cached hostfile
- * rpmforge: apt.sw.be
- * base: mirrors.greenmountainaccess.net
- rpmforge 100% |=========================| 1.1 kB 00:00
- base 100% |=========================| 2.1 kB 00:00
- Available Packages
- perl-File-Which.noarch 1.07-1.el5.rf rpmforge
- perl-UNIVERSAL-which.noarch 0.06-1.el5.rf rpmforge
- which.i386 2.16-7 base
- xml-commons-which.i386 1.3.02-0.b2.7jpp.10 base
- xml-commons-which-javadoc.i386 1.3.02-0.b2.7jpp.10 base
As you can see which is available for installed but not currently installed. Below is the yum command to install which and then list again to show how which will display once installed via yum. You can install which using “yum install which” on CentOS.
Install which On CentOS Linux Using Yum:
- [root@dev ~]# yum install which
- Loading "fastestmirror" plugin
- Loading mirror speeds from cached hostfile
- * rpmforge: apt.sw.be
- * base: mirrors.greenmountainaccess.net
- Setting up Install Process
- Parsing package install arguments
- Resolving Dependencies
- --> Running transaction check
- ---> Package which.i386 0:2.16-7 set to be updated
- --> Finished Dependency Resolution
- Dependencies Resolved
- =============================================================================
- Package Arch Version Repository Size
- =============================================================================
- Installing:
- which i386 2.16-7 base 23 k
- Transaction Summary
- =============================================================================
- Install 1 Package(s)
- Update 0 Package(s)
- Remove 0 Package(s)
- Total download size: 23 k
- Is this ok [y/N]: y
- Downloading Packages:
- (1/1): which-2.16-7.i386. 100% |=========================| 23 kB 00:00
- Running rpm_check_debug
- Running Transaction Test
- Finished Transaction Test
- Transaction Test Succeeded
- Running Transaction
- Installing: which ######################### [1/1]
- Installed: which.i386 0:2.16-7
- Complete!
Now once installed use yum to list the package and show it is installed using “yum list *WHICH*.
Use yum To List The which Package:
- [root@dev ~]# yum list *WHICH*
- Loading "fastestmirror" plugin
- Loading mirror speeds from cached hostfile
- * rpmforge: apt.sw.be
- * base: mirrors.greenmountainaccess.net
- Installed Packages
- which.i386 2.16-7 installed
- Available Packages
- perl-File-Which.noarch 1.07-1.el5.rf rpmforge
- perl-UNIVERSAL-which.noarch 0.06-1.el5.rf rpmforge
- xml-commons-which.i386 1.3.02-0.b2.7jpp.10 base
- xml-commons-which-javadoc.i386 1.3.02-0.b2.7jpp.10 base
As you can see which is now installed. Now running the intial command will succeed without issue.
Hi . I am using Red hat E 4 and i am working bops simulator.
after compile makefile , bops exe is producted and existed.
but by input command :bops test
Error is : bops :not command found.
thanks
Hello Amir,
I have no idea what bops is but it sounds as if the command is not in your PATH. You need to look at the location of the bops executable and add it to your PATH environment variable.
Thanks.
alex