which Command

The which command in Linux is used to locate the executable file associated with a given command. It searches the directories listed in the PATH environment variable and prints the full path of the executable if found. This is useful for determining which version of a command will be executed when multiple versions exist in different directories.

Syntax

which [options] [--] COMMAND ...

Description

which takes one or more arguments. For each of its arguments, it prints the full path of the executables that would be executed in the current environment if its argument were entered on the command line. It does this by searching the PATH environment variable.

Common uses include:

  • Finding the exact location of a command's executable
  • Verifying which version of a command is being used
  • Debugging PATH issues
  • Checking if a command is available on the system

Common Options

Option Description
-a, --all Print all matching executables in PATH, not just the first.
-s, --skip-alias Skip checking for aliases.
-i, --skip-functions Skip checking for shell functions.

Examples

Locate a command

which python

Displays the full path to the 'python' executable (e.g., /usr/bin/python).

Locate all matching executables

which -a ls

Shows all 'ls' executables found in the directories listed in your PATH.

Check for a command and suppress output

which git > /dev/null && echo "git is installed"

Checks if 'git' is installed without printing its path, then prints a message if it is.

Locate a command and ignore aliases/functions

which -s -i ls

Locates the 'ls' executable, ignoring any shell aliases or functions named 'ls'.

See also