which Command in Linux: Find Executable Locations

By 

Updated on

5 min read

Using the which command in Linux to find executable locations

The which command locates the full path of an executable by searching the directories listed in the PATH environment variable. It shows exactly which binary the shell will run when you type a command name.

This guide explains how to use which and how it compares to related commands such as type, whereis, and command -v.

Syntax

The syntax for the which command is:

txt
which [OPTIONS] COMMAND...
  • COMMAND — the name of one or more commands to locate.

Locate a Command

To find the full path of a command, pass its name as an argument. For example, to locate the ping command:

Terminal
which ping
output
/usr/bin/ping

which searches the directories in PATH from left to right and prints the path of the first match it finds.

You can pass multiple command names at once:

Terminal
which netcat uptime
output
/bin/netcat
/usr/bin/uptime

which prints one result per command, in the same order they were given.

When the same command name exists in more than one directory listed in PATH, which prints only the first match by default. To see all matches, use the -a option:

Terminal
which -a python3
output
/usr/bin/python3
/usr/local/bin/python3

This is useful when multiple versions of a command are installed. Usually one of the entries is a symlink to the other, but in some cases they are separate binaries.

What is PATH

PATH is an environment variable that tells the shell which directories to search for executables. It is a colon-separated list of absolute directory paths.

To view your current PATH:

Terminal
echo $PATH
output
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

The shell searches these directories in order from left to right. The first match wins, which is the same order which uses. To add a directory to PATH, see the PATH guide .

which vs. type, whereis, and command -v

Several commands serve a similar purpose. Here is how they differ:

  • which — searches PATH and prints the path of the executable. Does not find shell builtins or aliases.
  • type — a shell builtin that identifies how a name is interpreted: as a shell builtin, alias, function, or external command. Use type when you want to know what the shell will actually run.
  • whereis — locates the binary, source, and man page for a command. See the whereis guide .
  • command -v — POSIX-compliant alternative to which. Works in scripts and finds both builtins and external commands.

For shell scripts, prefer command -v over which because it is portable and handles builtins correctly.

Quick Reference

TaskCommand
Locate a commandwhich command
Locate multiple commandswhich cmd1 cmd2
Show all matches in PATHwhich -a command
View current PATHecho $PATH
Portable alternative (scripts)command -v command
Identify builtins and aliasestype command
Find binary, source, and man pagewhereis command

Troubleshooting

which returns no output
The command is either not installed, not in any PATH directory, or is a shell builtin or alias. Run type command to check — builtins like cd and echo are built into the shell and have no file path for which to find.

Wrong version of a command is being used
Run which -a command to see all matches in PATH. The first result is the one the shell uses. Reorder your PATH directories or use the full path to run a specific version.

which: no command in PATH
The command is not installed or its directory is not in PATH. Verify with echo $PATH and see the PATH guide to add a directory.

FAQ

What is the difference between which and type?
which searches PATH for an external executable file. type is a shell builtin that tells you how a name is resolved — it can identify aliases, functions, shell builtins, and external commands. For example, which cd returns nothing because cd is a builtin, but type cd correctly reports that.

Can which find shell aliases?
No. which only searches for executable files in PATH directories. To find aliases or functions, use type or alias.

Why does which python point to the wrong version?
The shell uses the first match in PATH. Run which -a python to see all installed versions and their paths, then adjust your PATH or use a version manager.

Is which available on all Linux systems?
which is available on virtually all Linux distributions. However, its behavior can differ slightly between implementations (GNU, BSD). For portable scripts, use command -v instead.

What does it mean when which returns two paths for the same command?
Run which -a command to see all matches. One is often a symlink to the other. Use ls -l on both paths to check.

Conclusion

The which command locates the executable the shell will run for a given command name by searching the directories in PATH. Use -a to see all matches when multiple versions are installed.

For shell scripts, prefer command -v for portability. To identify builtins and aliases, use type . To learn how command resolution depends on your environment, see how to add a directory to PATH .

If you have any questions, feel free to leave a comment below.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page