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
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
Displays the full path to the 'python' executable (e.g., /usr/bin/python).
Locate all matching executables
Shows all 'ls' executables found in the directories listed in your PATH.
Check for a command and suppress output
Checks if 'git' is installed without printing its path, then prints a message if it is.
Locate a command and ignore aliases/functions
Locates the 'ls' executable, ignoring any shell aliases or functions named 'ls'.