which Command
Beginner File Viewing & Searching man(1)Show the full path of shell commands
👁 8 views
📅 Updated: Mar 15, 2026
SYNTAX
which [OPTION] COMMAND
What Does which Do?
The which command locates the executable file associated with a command by searching the directories listed in the PATH environment variable. It shows which version of a program would run when you type its name.
which is essential for troubleshooting when multiple versions of a program are installed, verifying that a program is available, and checking the full path of commands for use in scripts and cron jobs.
which only searches the PATH directories and only finds executables. It does not find shell built-ins, aliases, or functions. For a more comprehensive lookup, use the type command.
which is essential for troubleshooting when multiple versions of a program are installed, verifying that a program is available, and checking the full path of commands for use in scripts and cron jobs.
which only searches the PATH directories and only finds executables. It does not find shell built-ins, aliases, or functions. For a more comprehensive lookup, use the type command.
Options & Flags
| Option | Description | Example |
|---|---|---|
| -a | Show all matching executables in PATH, not just first | which -a python |
| (no flags) | Show the first matching executable | which gcc |
| multiple | Check multiple commands at once | which python3 pip3 node npm |
| exit code | Returns 0 if found, 1 if not found | which docker && echo "Docker installed" |
| -s | Silent mode (some implementations) | which -s curl |
| in scripts | Use in conditionals for command checks | if which nginx > /dev/null 2>&1; then echo "installed"; fi |
Practical Examples
#1 Find command location
Shows the full path to the nginx executable.
$ which nginx
Output:
/usr/sbin/nginx
#2 Find all versions
Shows all python executables found in PATH, in order of precedence.
$ which -a python
Output:
/usr/bin/python
/usr/local/bin/python
#3 Check multiple commands
Shows paths for each command or an error if not found.
$ which python3 pip3 node npm#4 Verify command exists in script
Only runs docker --version if docker is found in PATH.
$ which docker > /dev/null 2>&1 && docker --version#5 Find compiler location
Locates build tools to verify development environment.
$ which gcc g++ make
Output:
/usr/bin/gcc\n/usr/bin/g++\n/usr/bin/make
#6 Debug PATH issues
If the wrong PHP version runs, this shows all php binaries in PATH order.
$ which -a phpTips & Best Practices
Use type instead for built-ins: which cannot find shell built-ins (cd, echo, etc.) or aliases. Use type -a command to find everything including built-ins and aliases.
Cron jobs need full paths: Cron has a minimal PATH. Use which to find the full path of commands for cron: which php returns /usr/bin/php.
Aliases not shown: which shows executables on disk, not shell aliases. If you aliased python to python3, which python still shows the original executable.
Frequently Asked Questions
How do I find where a command is installed?
Use which command_name to find the executable path. For example: which nginx shows /usr/sbin/nginx.
What is the difference between which and whereis?
which finds only the executable in PATH. whereis also finds man pages and source files, and searches standard locations rather than just PATH.
Why does which not find my command?
The command may not be in your PATH, or it might be a shell built-in/alias. Use type -a command for a comprehensive search. Check PATH with echo $PATH.
Related Commands
More File Viewing & Searching Commands
Master Linux with Professional eBooks
Curated IT eBooks covering Linux, DevOps, Cloud, and more
Browse Books →