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.

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.

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.

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.

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

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

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

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

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

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

Ensure the second script also carries executable permissions.

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

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.

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

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.

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

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).

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

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

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.

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.

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.

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

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.

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.

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.

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.