Linux which Command: Find Executables in PATH

The which command searches directories listed in PATH and prints a matching executable path. It is convenient for locating an external program, but shell aliases, functions, and builtins are better diagnosed with type or command -v.

This guide explains the basic syntax, key options, practical examples, related commands, common cautions, and frequently asked questions for which.

What Is the which Command?

A typical implementation checks PATH from left to right and reports the first executable file. However, which may be an external program or a shell-specific wrapper, so its options and treatment of aliases can vary. Its output is not always identical to what the current shell will execute.

Basic Syntax

which [OPTION]... COMMAND...

Options and edge cases can vary by implementation and version. Check which --help and man which on the target system before relying on implementation-specific behavior.

Basic which Usage

Find an executable path

The first matching executable in PATH is printed.

which python3

Check several commands

Provide multiple command names to inspect them in one invocation.

which sh awk sed

Show all PATH matches

Common implementations support -a for every matching executable in PATH order.

which -a python3

Limitations of Executable File Path Output

which shows the path of the executable file found in PATH. Depending on the system and shell, the same command may be located in different places.

which ls

Example output:

/usr/bin/ls

Shell aliases or functions may differ between the which output and the actual interpretation of the entered command. To check what the current shell will execute, use type ls or command -V ls.

Key Options

Option Purpose
-a, --all Print all PATH matches instead of only the first.
--skip-alias In some GNU which versions, skip alias handling.
--skip-functions In some GNU which versions, skip shell-function handling.
--version Identify the installed implementation and version.

Practical Examples

Inspect command priority

Compare all matches with the order of directories in PATH.

which -a python3
printf '%s\n' "$PATH"

Ask the shell what it resolves

These shell commands can identify aliases, functions, builtins, and external files.

type -a ls
command -V ls
command -v ls

Test availability in a script

For portable POSIX shell code, use the exit status of command -v rather than parsing which output.

if command -v curl >/dev/null 2>&1; then
  printf '%s\n' 'curl is available'
fi

which and Related Commands

Command Primary role Best fit
which Search PATH for external executables Quick path lookup
command -v Report shell command resolution Portable script checks
type Classify aliases, functions, builtins, and files Interactive diagnosis
whereis Search program-related locations Binaries, source, and manuals

Important Cautions

Aliases and functions may be missed

The shell can choose an alias or function before an external executable. Confirm with type.

Implementations differ

Do not assume optional flags are available in every environment.

A path is not a security verdict

Finding an executable does not verify its owner, permissions, integrity, or trustworthiness.

FAQ

Does no output mean the command is not installed?

It means no executable was found in PATH. The name could still be a shell builtin, function, or alias.

How do I show every matching path?

Use which -a COMMAND when the implementation supports it.

Should scripts use which?

For portable shell scripts, command -v is generally the better availability test.

How is whereis different?

Which searches PATH for executables; whereis searches known locations for binaries, source, and manual pages.

Related Resources

More in This Category
Linux bunzip2 Command: Decompress bzip2 Files

Linux bunzip2 Command: Decompress bzip2 Files

Learn how to decompress bzip2 Files with the Linux bunzip2 command, including practical examples, key options, and important precautions.

Linux du Command: Check File and Directory Disk Usage

Linux du Command: Check File and Directory Disk Usage

Learn how to use the Linux du command to measure and summarize disk space used by files and directories, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux top Command: Monitor Processes in Real Time

Linux top Command: Monitor Processes in Real Time

Learn how to monitor Processes in Real Time with the Linux top command, including practical examples, key options, and important precautions.

Linux csplit Command: Split Files at Lines or Patterns

Linux csplit Command: Split Files at Lines or Patterns

Learn how to split Files at Lines or Patterns with the Linux csplit command, including practical examples, key options, and important precautions.

Linux touch Command: Create Files and Change Timestamps

Linux touch Command: Create Files and Change Timestamps

Learn how to use the Linux touch command to create empty files and change access or modification timestamps, including reference times, date formats, and symlink handling.

Linux cd Command: Change the Current Directory

Linux cd Command: Change the Current Directory

Learn how to use the Linux cd command to move between directories using absolute, relative, home, and previous paths, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux zip Command: Create ZIP Archives

Linux zip Command: Create ZIP Archives

Learn how to create ZIP Archives with the Linux zip command, including practical examples, key options, and important precautions.

Linux apk Command: Manage Packages on Alpine Linux

Linux apk Command: Manage Packages on Alpine Linux

Learn how to manage Packages on Alpine Linux with the Linux apk command, including practical examples, key options, and important precautions.

Linux printf Command: Print Values with a Defined Format

Linux printf Command: Print Values with a Defined Format

Learn how to print Values with a Defined Format with the Linux printf command, including practical examples, key options, and important precautions.

Linux last Command: Review Login and Reboot History

Linux last Command: Review Login and Reboot History

Learn how to use the Linux last command to review user login sessions, system reboots, and shutdown history, with essential options, practical examples, output interpretation, and common troubleshooting tips.