The which command in Linux prints the full path of the executable that would run when you type a command, by searching the directories listed in your $PATH. Use which <name> for the first match or which -a <name> to show all matches.
It’s ideal for verifying versions, resolving path conflicts, and debugging “command not found” errors. If you’re new to Linux or managing servers, mastering the which command in Linux will save time and prevent misconfigurations.
In this guide, you’ll learn what which does, how it differs from alternatives like command -v and whereis, and see practical examples from real world server administration especially useful for web hosting environments.
What is the which Command in Linux?
The which command tells you the exact path of the executable that the shell would run for a given command name.

It searches the directories in the $PATH environment variable in order and returns the first match (or all matches with -a). This is essential for confirming which binary actually executes when multiple versions are installed.
In hosting and DevOps workflows, which helps you confirm the PHP, Python, Node.js, or Git version you’re invoking critical when paths differ per user, virtual environment, or container.
Syntax and Common Options
which [options] name [name ...]
Frequently used options (GNU which; options may vary slightly across distros):
- -a, –all: Show all matching executables in $PATH.
- –skip-alias / –skip-functions: Ignore shell aliases or functions (when supported).
- –read-alias / –read-functions: Print definitions of aliases/functions (GNU which feature).
Shell built ins, aliases, and functions aren’t always handled consistently by which across platforms. For shell aware checks, command -v or type is often more reliable.
Practical Examples of which
1. Find the path of a command
which ls
# /usr/bin/ls
which nginx
# /usr/sbin/nginx (example)
This shows the exact binary that will run when you type the command.
2. Show all matches along $PATH
which -a python
# /usr/bin/python
# /usr/local/bin/python (example)
which -a php
# /usr/bin/php
# /usr/local/bin/php
If multiple versions are installed, -a reveals the full resolution order. This is vital when the wrong version appears first in $PATH.
3. Check multiple commands at once
which git curl unzip
# /usr/bin/git
# /usr/bin/curl
# /usr/bin/unzip
which returns a nonzero exit code if one or more commands aren’t found (useful in scripts).
4. Aliases and functions caveat
# You may have an alias:
alias ll='ls -alF'
which ll
# Might print nothing or the ls path; behavior varies by system.
# Better:
type -a ll
# ll is aliased to `ls -alF`
Because which is not a shell built in, it may not fully understand aliases or functions. Prefer type or command -v for shell aware results.
5. which with sudo and PATH differences
which nginx
# /usr/sbin/nginx
sudo which nginx
# which: no nginx in (...root's PATH...)
# or a different path than your user
sudo may use a different PATH for root, so sudo which can yield different or empty results. If you only want to run which with your current PATH but elevated privileges, do:
sudo env "PATH=$PATH" which nginx
Security tip: Be cautious when modifying PATH under sudo. Use explicit paths in automation where possible.
which vs. command -v vs. type vs. whereis
Several commands help discover “what will run,” but they serve different purposes. Here’s when to use each:
- which: Finds the first (or all) executables in $PATH. Great for quick path checks and resolving version conflicts.
- command -v: POSIX-compliant; prints how the shell resolves a name (covers built ins, functions, aliases, and executables). Prefer this in portable scripts.
- type -a: Bash/Zsh built in that explains whether a name is an alias, function, builtin, or file, and shows all locations.
- whereis: Searches standard locations for binaries, source, and man pages. Not tied to $PATH; broader and less precise for execution order.
# Portable and reliable in scripts:
command -v nginx >/dev/null 2>&1 && echo "nginx is available"
Understanding $PATH and Execution Order
$PATH is a colon separated list of directories the shell searches to find executables. which uses this order to determine the command that will run first.
echo $PATH
# /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin (example)
To prioritize a custom directory useful when you install a newer PHP or Node.js—prepend it to PATH:
# Temporary (current shell session)
export PATH="/opt/php8.2/bin:$PATH"
# Persist for a user (Bash)
echo 'export PATH="/opt/php8.2/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
After changing PATH, verify with which -a to confirm the resolution order is correct.
Exit Codes and Using which in Scripts
GNU which typically returns:
- 0: All specified commands were found.
- 1: Some commands were not found.
- 2: Invalid options or a serious error occurred.
# Example: Ensure required tools exist before deployment
if which git >/dev/null 2>&1 && which rsync >/dev/null 2>&1; then
echo "Deps OK"
else
echo "Missing required tools" >&2
exit 1
fi
For POSIX portability, command -v is often recommended:
if command -v php >/dev/null 2>&1; then
php -v
else
echo "PHP not installed or not in PATH" >&2
fi
Troubleshooting which
Command found but not executable
If a file exists in PATH but lacks execute permissions for your user, which may not show it. Fix permissions or adjust PATH.
Hash cache mismatch (Bash)
The shell may cache command locations. If you replace a binary, clear the cache:
hash -r
Different behavior with sudo or non login shells
Login vs non login shells and sudo policies can produce different PATH values.
Compare environments:
printf "User PATH: %s\n" "$PATH"
sudo env | grep -E '^PATH='
Real World Hosting Use Cases
Pick the correct PHP for CLI and cron
Servers often host multiple PHP versions (e.g., 7.4, 8.1, 8.2). Use which -a php to see available binaries and update PATH or call the absolute path in crontab to avoid running the wrong PHP version.
Validate pip vs pip3 during deployments
In Python environments, pip may point to Python 2 while pip3 targets Python 3. Confirm with which pip and which pip3, or rely on python3 -m pip to ensure the correct interpreter installs dependencies.
Confirm Node and npm paths in CI/CD
When using Node Version Manager (nvm) or custom installs, check which node and which npm inside build steps to guarantee the expected version is used during asset compilation.
At YouStable, we recommend explicitly setting PATHs in automation and using which -a during build validation to prevent version drift between staging and production. Our managed hosting stacks keep PATH consistent across users to reduce surprises in cron, SSH, and web server contexts.
Security and Best Practices
- Never put the current directory (.) at the start of PATH; it can enable accidental or malicious command execution.
- Prefer absolute paths in privileged scripts (e.g., /usr/bin/rsync) to avoid PATH hijacking.
- When using sudo, understand what PATH is applied; consult /etc/sudoers and secure_path settings.
- Use command -v in scripts for portability and clarity when checking command availability.
FAQ’s
1. What does the which command do in Linux?
which searches the directories listed in $PATH and prints the path of the executable that would run for a given command name. Use which -a to list all matches in search order.
2. Why does which show nothing for my command?
Either the command isn’t installed, it’s not in your $PATH, or it’s an alias/function that which doesn’t recognize. Try command -v or type -a. Also verify permissions and shell type (login vs non-login).
3. What’s the difference between which and whereis?
which uses $PATH to show what will execute; whereis scans standard locations to find binaries, source, and man pages. whereis is broader but not indicative of execution order.
4. Is which POSIX compliant? Should I use command -v instead?
which isn’t specified by POSIX and behaves inconsistently across systems. command -v is POSIX-compliant and preferred in portable scripts, especially when you need to detect aliases, functions, and built ins reliably.
5. How do I fix PATH so which finds the right version?
Prepend the desired bin directory to PATH, then verify with which -a. Example: export PATH=”/opt/php8.2/bin:$PATH”. Persist changes in your shell profile (e.g., ~/.bashrc) and reload the shell.
Mastering the which command in Linux is foundational for clean, predictable server operations.
Use it alongside command -v and type to understand exactly what your shell will execute and keep your environments consistent and secure. If you want a hosting platform where PATH and tooling are configured right out of the box, YouStable’s managed environments are built for reliability and clarity.