The which command displays the complete path of a specified command by searching the directories assigned to the PATH variable. For example, upon entering which cat, the following output is displayed: /bin/cat.
$ which cat /bin/cat
The which command can therefore help you locate where a program has been installed in case you need to modify this. It can also help you identify which version of a command you’re using if there are multiple binaries of the command stored in different locations, one of which may be more ideal. By identifying where a command is running from, you can troubleshoot unexpected behavior from that command.
Syntax
The syntax of the which command is:
$ which [options] {program names}
If you encounter below error while running the which command:
which: command not found
you may try installing the below package as per your choice of distribution:
Distribution | Command |
---|---|
OS X | brew install debianutils |
Debian | apt-get install debianutils |
Ubuntu | apt-get install npm |
Alpine | apk add which |
Arch Linux | pacman -S which |
Kali Linux | apt-get install debianutils |
CentOS | yum install which |
Fedora | dnf install which |
Raspbian | apt-get install debianutils |
which Command Examples
1. Search the PATH environment variable and display the location of any matching executables:
$ which executable
2. If there are multiple executables which match, display all:
$ which -a executable
Final Thoughts
The which command locates an executable file in your shell’s search path. You can even find the which program itself:
$ which which /usr/bin/which
If several programs in your search path have the same name (for example, /usr/bin/who and /usr/local/bin/who), which reports only the first.