Linux which Command

Locate Executable in PATH - 10 Practical Examples with Detailed Explanations

Example 1

Find Command Location

$ which ls
/usr/bin/ls
Shows the full path to the executable that would be executed when you type the command. Searches through directories in your PATH environment variable in order. Returns the first match found.
PATH Dependent: Shows what would actually run based on your PATH
Example 2

Check Multiple Commands

$ which python python3 pip
/usr/bin/python /usr/bin/python3 /usr/bin/pip
Finds the location of multiple commands at once. Each result appears on a separate line. Useful for quickly checking the paths of several related commands or verifying installations.
Batch Check: Verify multiple command locations in one go
Example 3

Show All Matches in PATH

$ which -a python
/usr/bin/python /usr/local/bin/python /opt/python/bin/python
Displays all matching executables in PATH using the '-a' option, not just the first one. Shows every location where the command exists. Helpful when multiple versions are installed.
Multiple Versions: Find all installed versions of a command
Example 4

Check if Command Exists

$ which docker
/usr/bin/docker
Verifies if a command is available in your PATH. If the command exists, which returns its path with exit status 0. If not found, it returns nothing with exit status 1. Perfect for prerequisite checks in scripts.
Script Usage: Use exit status to test command availability
Example 5

Distinguish Aliases from Binaries

$ alias ll='ls -la' $ which ll $ type ll
ll: aliased to ls -la
Shows that which only finds actual executables, not shell aliases or functions. Use the 'type' command instead to identify aliases, functions, and builtins. which is limited to files in PATH.
Alternative: Use 'type' command for aliases, builtins, and functions
Example 6

Use in Scripts for Validation

$ if which docker > /dev/null 2>&1; then echo "Docker is installed" else echo "Docker not found" fi
Docker is installed
Uses which in conditional statements to check command availability. Redirects output to /dev/null to suppress the path display. Exit status determines if command exists. Essential pattern for dependency checking.
Exit Status: 0 if found, 1 if not found
Example 7

Identify Command Conflicts

$ which -a node $ echo $PATH
/usr/local/bin/node /usr/bin/node /PATH/includes/these/directories
Helps identify why a different version of a command is running than expected. Shows all instances and PATH order. Useful for debugging version conflicts when multiple installations exist.
Debugging: Find why wrong version is executing
Example 8

Verify Symlinks

$ which python $ ls -l $(which python)
/usr/bin/python lrwxrwxrwx 1 root root 7 Oct 18 10:00 /usr/bin/python -> python3
Combines which with ls to reveal if a command is a symbolic link. Shows what the command actually points to. Important for understanding system configuration and version management.
Common Pattern: Many commands are symlinks to versioned binaries
Example 9

Compare which vs whereis

$ which grep $ whereis grep
/usr/bin/grep grep: /usr/bin/grep /bin/grep /usr/share/man/man1/grep.1.gz
Demonstrates the difference between which and whereis. which shows only the executable in your PATH that would run. whereis shows all standard locations including man pages. which is PATH-aware, whereis is not.
Choose Wisely: which for active executable, whereis for all locations
Example 10

Trace Command Execution Path

$ which -a bash $ echo $SHELL
/usr/bin/bash /bin/bash /usr/bin/bash
Identifies which shell or interpreter will be used. Combined with environment variables, helps understand exactly what executes when you run a command. Useful for shell scripting and understanding your environment.
Environment: Your PATH order determines which executable runs