Mastering Linux which: Find Binary Paths & Understand PATH

Introduction

Pinpointing the exact disk location of an executing binary is an indispensable skill when administering Linux environments or debugging configuration discrepancies. This hands-on tutorial walks through the practical mechanics of the which command, dissects its essential runtime flags, and explores how it interacts with the system’s $PATH variable.

Core Mechanics: Resolving Single Executable Paths with which

Whenever an executable name is passed to which, the utility scans directories defined across the $PATH environment variable in sequential order, returning the absolute filesystem location of the first match encountered.
Running which bash, for instance, reveals that the primary shell binary resides at /usr/bin/bash.
⚙️ [Essential Flags]

-a : Inspects the entire $PATH string and prints every matching executable rather than stopping at the first match.
-s : Suppresses standard output entirely, returning solely an exit status code to indicate whether the target was located.

Running which bash in terminal

Invoking which python3 identifies the active Python interpreter resolved by your current shell session.
The terminal output verifies that /usr/bin/python3 serves as the runnable binary ready for immediate invocation.

Locating python3 interpreter path

Querying which ls pinpoints the underlying binary used for directory listings.
The system confirms /usr/bin/ls, highlighting that the utility is managed under standard system package paths.

Resolving standard ls utility path

Detecting Duplicate Binaries Across Directories (-a Flag)

To simulate how multiple executables sharing identical names are evaluated, set up a temporary sandbox directory named /tmp/custom_bin.

Creating custom binary directory

Inside this new location, initialize a shell script named mytool featuring a standard bash shebang.

Adding bash shebang to custom mytool

Append an echo statement that clearly identifies this specific script instance during runtime.

Appending custom identifier message

Apply executable permissions to mytool via chmod +x so the shell can launch it directly.

Granting execute permissions

Next, create an alternate secondary directory called /tmp/extra_bin to host a duplicate script.

Creating secondary directory

Populate this directory with another script bearing the exact same name, mytool, and declare its interpreter header.

Writing shebang for extra mytool

Include an unmistakable print message to differentiate this second script from the first one.

Adding distinct message to extra script

Ensure the second script also carries executable permissions.

Enabling execution on secondary script

Prepend both custom directories to your active $PATH string to establish clear resolution priority.

Updating PATH with custom directories

Running a standard which mytool query produces only /tmp/custom_bin/mytool, because the command halts processing immediately after finding the first candidate in the $PATH chain.

Locating primary mytool binary

Passing the -a argument via which -a mytool prompts the utility to traverse every directory in $PATH, exposing all matching binaries in priority order.

Listing all occurrences of mytool with -a

Understanding PATH Resolution Precedence and its Impact on which

Inspecting the full $PATH variable illustrates how colon-separated (:) directory paths dictate the evaluation order.
Entries positioned earliest in this string take absolute precedence whenever the shell or which searches for target executables.

Displaying PATH environment variable

Re-inserting /tmp/custom_bin at the very beginning of $PATH refreshes precedence so this directory is evaluated first.

Prepending custom directory to PATH

Executing which mytool confirms that resolution honors the revised search priority.
If no path is returned, verify both directory registration and file execute permissions (+x).

Verifying mytool resolution under updated PATH

Unsetting $PATH completely strips the shell of any automated binary lookup paths.

Unsetting PATH variable

Restore core functionality by repopulating $PATH with standard Linux system binary directories.

Restoring default system PATH

With search paths restored, querying which grep tracks down the text search tool.
The shell resolves /usr/bin/grep instantly within the restored system path hierarchy.

Resolving grep utility path

Limitations of which: Comparing with type and whereis

Issuing which cd returns a blank line without producing any output.
Because cd operates as an internal shell builtin rather than an external disk binary, which cannot resolve it within $PATH directory trees.

Attempting to locate cd with which

Checking type cd reveals how the shell actually interprets the directive.
The command outputs cd is a shell builtin, clearly explaining why it does not exist as a standalone file on disk.

Inspecting cd using type command

To examine how aliases behave under scrutiny, define a shortcut named ll using alias ll='ls -alF'.

Creating custom shell alias ll

Running which ll yields nothing once again.
Since which only traverses physical files within $PATH, runtime aliases living exclusively in shell memory remain completely invisible to it.

Searching for alias with which

In contrast, executing type ll unmasks the alias immediately.
The type builtin seamlessly detects both internal shell commands and custom aliases, displaying their exact definitions.

Revealing alias definition with type

Finally, running whereis ls broadens the search scope across the system.
Unlike which, whereis rapidly locates not only binary executables but also corresponding source files and manual pages (man entries) across standard system locations.

Locating binaries and man pages using whereis

Mastering how which traverses $PATH alongside alternatives like type and whereis provides critical visibility into command execution.
Leveraging these search fundamentals streamlines diagnosing version conflicts, missing permissions, and command resolution quirks across any Linux workflow.