How to Use the which and whereis Commands to Locate Binaries in Linux

Executing software from the Linux terminal relies on the shell locating the corresponding executable file within your system directory hierarchy. However, when troubleshooting version discrepancies—such as wondering whether typing python invokes the system-default Python 3.10 interpreter, an Anaconda environment binary, or a custom pyenv build—identifying the exact binary path being executed is vital. Linux provides two fundamental command-line inspection utilities: which and whereis. Alongside the shell built-in type, understanding how these tools inspect system paths allows system administrators and developers to trace command origins and diagnose PATH conflicts in seconds.

The Difference Between which, whereis, and type

While all three tools locate executable binaries, each operates on a distinct technical mechanism and search scope:

  • which: Searches strictly within the directories defined in your active $PATH environment variable. It returns the exact single executable binary that will launch when you type that command name in your current shell.
  • whereis: Bypasses the $PATH variable and conducts a fast index lookup across standard Unix system directories (/bin, /sbin, /usr/bin, /usr/share/man). It reports not only the binary, but also its manual (man) documentation pages and source code repositories.
  • type: A built-in shell utility that determines whether a command is an external disk binary, an active alias, an internal shell builtin (like cd or export), or a user-defined shell function.

Using the which Command to Find Active Binaries

To pinpoint the exact executable that runs when you invoke a command:

which git

Output: /usr/bin/git

If you suspect multiple versions of a tool are installed across different directories in your PATH (e.g., in /usr/local/bin and /usr/bin), pass the -a (all) flag to display every matching binary path in execution order:

which -a python3

Output:

/usr/local/bin/python3
/usr/bin/python3

Because the shell evaluates PATH from left to right, the first result listed is the primary executable that takes execution precedence.

Using the whereis Command for Comprehensive System Discovery

To locate a program’s binary alongside its system documentation and manual pages:

whereis nginx

Output: nginx: /usr/sbin/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz

You can filter whereis output using dedicated command flags:

  • Search for Binaries Only (-b):
    whereis -b nginx
    Output: nginx: /usr/sbin/nginx
  • Search for Manual Pages Only (-m):
    whereis -m nginx
    Output: nginx: /usr/share/man/man8/nginx.8.gz
  • Search for Source Code Only (-s):
    whereis -s nginx

Command Comparison: which vs. whereis vs. type

Utility Search Source Identifies Aliases? Identifies Man Pages? Primary Strength
which Active $PATH variable No (binary only) No Pinpoints exact runtime executable in current environment
whereis Standard Unix directories No Yes (man pages & source) Fast system-wide discovery of packages and documentation
type Shell internal symbol table Yes (shows alias expansion) No Reveals whether command is alias, function, or builtin

Why type -a Is Essential for Aliases and Builtins

A common trap for Linux users is attempting to use which on internal shell commands like cd, alias, or history. Because these are built into Bash itself rather than separate files on disk, which will report no output or fail with an error code.

Always use type -a when inspecting ambiguous commands:

type -a ll

Output: ll is aliased to 'ls -lah --color=auto'

type -a cd

Output: cd is a shell builtin

Frequently Asked Questions

Why does which command return an exit code of 1?

If which returns no output and exits with code 1, it means no executable matching that name was found within any of the directories currently listed in your $PATH environment variable. Either the software is not installed, or its installation folder is missing from PATH.

Can whereis search custom directories outside standard paths?

Yes. You can supply custom directory lists to whereis using the -B (for binary paths) or -M (for manual paths) flags followed by -f to terminate the directory list.

Is which a binary or a shell built-in?

On modern Debian, Ubuntu, and Fedora distributions, which is typically a standalone binary located at /usr/bin/which or an alias to type -P depending on your active shell distribution.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.