Where to start First steps track · 15 stages
Linux Labs

which / type / whereis — where a command lives

4 min bash

Find out which binary will run, whether it's a builtin or an alias, where it lives, and its man page.

Cheat sheet

4 commands Printable version
  • type -a cmd alias/builtin/function/file — every match
  • command -v cmd a path or nothing (for scripts)
  • which cmd just the path to the file
  • whereis cmd binary + man page + sources

Overview

When you type a command, the shell searches for it in the directories in $PATH (see env-vars). These commands answer the question "what exactly will run, and from where":

  • type (a bash builtin) — the most accurate: distinguishes alias, function, builtin, and file.
  • command -v — a portable way to learn the path/existence (for scripts).
  • which — a separate program, shows only the path to a file (doesn't see aliases/builtins).
  • whereis — binary + sources + man page.

Options

5

type -a and which -a show every match along $PATH, not just the first one that would actually run — useful when a command behaves unexpectedly and you suspect a version conflict. Plain which without -a stops at the first file found and says nothing about aliases or functions.

Option What it does Example
-a which — list every match in $PATH, not just the first one. which -a python3
-a type — show every place the command was found, in search order. type -a python3
-t type — print a single word (alias/keyword/function/builtin/file). type -t cd
-b whereis — search only for the binary, skipping man pages and sources. whereis -b nginx
-p type — print the file path only if it is an external command. type -p python3

Nothing found. Try a shorter query.

Recipes

1

Examples

bash
type ls               # ls is aliased to `ls --color=auto`  (sees the alias!)
type -a python3       # EVERY match along PATH (where else it lives)
type cd               # cd is a shell builtin
command -v git        # /usr/bin/git  (or nothing + exit code 1, if missing)
which python3         # /usr/bin/python3
whereis nginx         # nginx: /usr/sbin/nginx /etc/nginx /usr/share/man/...

A reliable "does this command exist" check in a script:

bash
if command -v docker >/dev/null 2>&1; then
  echo "docker is available"
fi

Troubleshooting

4

which found it, but the command says "not found" (or the reverse)

which doesn't know about aliases/builtins/functions and reads its own $PATH, which may differ from the current shell's. Trust type — it asks the shell itself.

The "wrong" version runs

type -a name shows every match along PATH in search order — the first one is what runs. Fix the order in $PATH or call it by full path.

command not found even though the package is installed

The binary isn't in $PATH (often /usr/local/sbin, ~/.local/bin). Check echo $PATH and whereis name — see env-vars.

After installing a new version, the old one still runs

Bash caches the resolved path to a command in a hash table for the session. Installing a new version alongside the old one (or through a different package manager) doesn't reset this cache automatically: hash -r makes bash search $PATH again, hash -t name shows what's currently cached.

See also

  • env-vars — env, export, $PATH, ~/.bashrc vs profiles; why "command not found".
  • man-help — How to quickly learn a command's options — man, --help, apropos, tldr.
  • alias — Short names for long commands; temporary and permanent via ~/.bashrc; why aliases aren't visible in scripts.
Loading…