which Command Guide
which shows which executable the shell would run when you type a
command name. It searches the directories in your PATH, in order, and
prints the first match — making it the quick answer to “where is this
command installed?” and “which version am I actually running?”
What is which, and Why Use It?
You install Python 3.12 and the system Python 3.10. You activate a
virtualenv. You install a new tool with pip --user. Now you type
python and wonder: which one runs? which tells you — the first
matching executable found in PATH, which is exactly the one your shell
will execute.
It’s also handy for confirming a tool is installed at all, and for debugging “why is my script using the wrong version?” problems.
# Where is python?
which python
# Where is git?
which git
# Is rsync installed?
which rsync
# Check all the shells you have
which bash zsh sh
Basic Usage
# Single command
which ls
# /usr/bin/ls (or /bin/ls on merged-usr systems)
# Several at once
which ls cd git
# ls is a shell builtin — which won't find it!
# Nothing printed = not found in PATH
which nonexistent-command
If the command isn’t in PATH, which prints nothing (and on some
systems exits with status 1). That’s your signal it’s either not
installed, or it lives somewhere outside PATH.
which vs Shell Builtins
Important subtlety: which only looks for external executables. It
does not know about shell builtins like cd, echo, or export. For
those, use type instead:
which cd # prints nothing — cd is a builtin
type cd # cd is a shell builtin
which echo # often /bin/echo, but the shell uses its builtin first
type -a echo # shows both the builtin and /bin/echo
Rule of thumb: use which for “where is the binary,” use type for
“what is this name, really.”
Checking What You’re Actually Running
which is the first step in version debugging:
# Which python will my scripts use?
which python3
# Is my user-installed version taking precedence?
which pip
# /home/you/.local/bin/pip ← user install beats /usr/bin/pip
# After changing PATH, confirm the new one wins
PATH="$HOME/opt/bin:$PATH"
which node
This pairs directly with the PATH page —
which shows the result of the PATH lookup order that page explains.
which in Scripts
# Check if a command exists before using it
if which ffmpeg > /dev/null 2>&1; then
echo "ffmpeg is installed"
else
echo "please install ffmpeg"
fi
# Find the binary path for scripting
GIT=$(which git)
"$GIT" --version
Note: command -v is the POSIX-blessed alternative and works in every
shell, including minimal ones:
command -v ffmpeg >/dev/null 2>&1 && echo "found"
whereis and locate: The Bigger Search
which only finds executables in PATH. When you need more:
- whereis — binary, source, and man page locations
locate— search the whole filesystem’s database (fast, but the database may be stale)find / -name ...— exhaustive, slow, always current
# whereis also shows man pages and sources
whereis git
# locate finds files anywhere (if the database is fresh)
locate nginx.conf
Common Pitfalls
- Silent success for builtins.
which cdprints nothing but exits 0 on some systems. Usetypefor anything that might be a builtin or alias. - Stale shell cache. After installing something, your shell may still
remember the old PATH result. Run
hash -r(bash) to clear the cache, or open a new shell. - Aliases fool you.
whichignores aliases;type -ashows them. Ifalias ll='ls -la'exists,which llsays nothing butllstill works. See aliases. - Not a security tool.
whichshows what your shell would run; a different user or a cron job may have a different PATH and a different answer. - 32/64-bit confusion. Some systems have
python,python3,python3.12all in PATH —which pythonanswers only the first.
Related Pages
- type — what a name really is: builtin, alias, or file
- whereis — binary + source + man page locations
- PATH — the search order
whichreflects - man — read the docs of whatever you find
- environment-variables — where PATH comes from
Cheatsheet
| Command | What it does |
|---|---|
which python3 | Full path of the python3 that would run |
which ls cd git | Check several commands at once |
which nonexistent | Prints nothing if not in PATH |
command -v cmd | POSIX way to check existence |
which cmd > /dev/null && echo ok | Existence check in scripts |
hash -r | Clear the shell’s command cache |
type -a cmd | Show builtins, aliases, and all PATH copies |