which

system managementlinux
The which command is one of the most frequently used commands in Linux/Unix-like operating systems. which The which command locates the executable file associated with a given command by searching the directories listed in the PATH environment variable. It helps determine which version of a program will be executed when you type a command in the terminal.

Quick Reference

Command Name:

which

Category:

system management

Platform:

linux

Basic Usage:

which [options] [arguments]

Common Use Cases

  • 1

    Command location

    Find the location of executable files in the system

  • 2

    Path resolution

    Determine which version of a command is being executed

  • 3

    Scripting

    Use in shell scripts to dynamically locate commands

  • 4

    Troubleshooting

    Diagnose issues with command execution

Syntax

which [OPTION]... COMMAND...

Options

Option Description
-a, --all Print all matching executables in PATH, not just the first
-i, --read-alias Read aliases from stdin, reporting matching ones on stdout (not in all versions)
-s, --silent, --quiet Silent mode, only return exit status (0 if found, 1 if not found)
--skip-alias Ignore option -i and skip alias processing
--skip-functions Ignore shell functions (not in all versions)
--skip-dot Skip directories in PATH that start with a dot
--skip-tilde Skip directories in PATH that start with a tilde and executables which reside in the HOME directory
--show-dot Don't skip directories in PATH that start with a dot
--show-tilde Convert HOME directory to a tilde in output
--tty-only Stop processing options on the right if not on tty
-v, --version Show version information
-h, --help Display help information

Examples

How to Use These Examples

The examples below show common ways to use the which command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

#

Basic Examples:

which ls

Show the full path of the ls command.

which python

Find the location of the python interpreter.

which bash zsh sh

Find the full paths of multiple shells at once.

Advanced Examples:

which -a python

Show all instances of python in PATH, not just the first one.

which -a java | xargs ls -l

Find all instances of the Java command and show detailed file information for each.

PATH=/custom/path:/bin which program

Temporarily modify the PATH to check where a program would be found with a different PATH.

which -i gcc

On some systems, use the -i flag to make which search for all executables, ignoring any aliases.

type python

While not the which command, the "type" command is a shell builtin that provides similar functionality and shows if something is an alias, function, or executable.

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

Key Points:

  • The which command searches through the directories listed in the PATH environment variable
  • It returns the first matching executable found in the PATH, unless the -a option is used
  • If a command is not found, which typically returns a non-zero exit status
  • Unlike locate or find, which only searches in the PATH environment variable
  • Which only looks for executable files, not data files or documentation
  • Some shells have a builtin version of which that may behave differently

PATH Environment Variable:

  • The PATH variable contains a colon-separated list of directories to search for executables
  • Directories are searched in the order they appear in the PATH variable
  • You can view your PATH with echo $PATH
  • Common directories in PATH include /bin, /usr/bin, /usr/local/bin, and /home/username/bin
  • When you type a command, the shell searches through these directories to find the program to execute

Common Use Cases:

  • Finding which version of a program will be executed when multiple versions are installed
  • Confirming that a program is installed and available in the PATH
  • Checking the location of a command to determine if it's the expected version
  • Troubleshooting command not found errors by verifying executable locations
  • Determining if a command is in the standard system path or a custom path

Related Commands:

  • whereis - Locate the binary, source, and manual pages for a command
  • type - Shell builtin that indicates how a command would be interpreted
  • find - More general file searching tool not limited to the PATH
  • locate - Find files by name anywhere on the system using a database
  • command - Execute a command bypassing any shell functions or aliases

Implementation Notes:

  • Different distributions may have slightly different versions of which
  • The GNU version of which is part of the GNU Core Utilities
  • Some shells like bash have their own implementation as a shell builtin
  • Bash's builtin type command can provide more information than which
  • Which does not usually show aliases or shell functions, only executable files

Tips & Tricks

1

Use the -i option for case-insensitive searching

2

Use the -l number option to limit the number of results

3

Use the -e option to only display entries that exist at the time which is run

4

Use the -r regexp option to search using a basic regular expression

5

Use the -d path option to specify a custom database path

Common Use Cases

Command location

Find the location of executable files in the system

Path resolution

Determine which version of a command is being executed

Scripting

Use in shell scripts to dynamically locate commands

Troubleshooting

Diagnose issues with command execution

Security

Prevent execution of malicious commands

Related Commands

These commands are frequently used alongside which or serve similar purposes:

Use Cases

1

Command location

Find the location of executable files in the system

2

Path resolution

Determine which version of a command is being executed

3

Scripting

Use in shell scripts to dynamically locate commands

4

Troubleshooting

Diagnose issues with command execution

5

Security

Prevent execution of malicious commands

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the which command works in different scenarios.

$ which
View All Commands