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 install Command: Copy Files and Set Attributes

Linux install Command: Copy Files and Set Attributes

Learn how to use the Linux install command to copy files while setting modes, owners, groups, timestamps, and destination directories in deployment scripts.

Linux fg Command: Bring a Shell Job to the Foreground

Linux fg Command: Bring a Shell Job to the Foreground

Learn how to bring a Shell Job to the Foreground with the Linux fg command, including practical examples, key options, and important precautions.

Linux Tutorial / Search and Process Text with grep, sort, uniq, cut, and wc

Linux Tutorial / Search and Process Text with grep, sort, uniq, cut, and wc

Practice searching, sorting, grouping, extracting, and counting text with grep, sort, uniq, cut, and wc, including reproducible output and key caveats.

Linux nslookup Command: Query DNS Names and Records

Linux nslookup Command: Query DNS Names and Records

Learn how to query DNS Names and Records with the Linux nslookup command, including practical examples, key options, and important precautions.

Linux stat Command: Display Detailed File Metadata

Linux stat Command: Display Detailed File Metadata

Learn how to use the Linux stat command to inspect file size, permissions, ownership, timestamps, inode, and filesystem metadata, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux type Command: Identify How a Command Is Resolved

Linux type Command: Identify How a Command Is Resolved

Learn how to use the Linux type command to identify aliases, functions, built-ins, keywords, and executable paths, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux ssh Command: Connect to a Remote Host Securely

Linux ssh Command: Connect to a Remote Host Securely

Learn how to connect to a Remote Host Securely with the Linux ssh command, including practical examples, key options, and important precautions.

Linux umask Command: Control Default File Permissions

Linux umask Command: Control Default File Permissions

Learn how to control Default File Permissions with the Linux umask command, including practical examples, key options, and important precautions.

Linux Tutorial / File Types and Metadata: ls, file, stat, and readlink

Linux Tutorial / File Types and Metadata: ls, file, stat, and readlink

Learn what ls, file, stat, and readlink each reveal about a Linux file, including type, size, timestamps, and symbolic-link targets.

Linux w Command: Show Logged-In Users and Their Processes

Linux w Command: Show Logged-In Users and Their Processes

Learn how to show Logged-In Users and Their Processes with the Linux w command, including practical examples, key options, and important precautions.