Home > File & Directory Management > which

Guide to the 'which' Command: Finding the Location of Executable Commands

The `which` command is used in Linux and Unix-like operating systems to locate the executable file associated with a given command in the directories listed in the `PATH` environment variable, essentially determining which command will be executed. It is very useful for checking the existence of programs in shell scripts or for figuring out which version of a program with the same name will be executed. Through this guide, you will learn how to use the `which` command and its main options.

Overview of 'which'

The `which` command tells you the location of the executable file that corresponds to the command entered by the user in the terminal (e.g., `ls`, `python`, `nginx`). It works by searching through the directories listed in the `PATH` environment variable in order and outputs the absolute path of the first executable file found. If the command cannot be found, it either outputs nothing or returns an error message.

Main Role of 'which'

The `which` command is primarily used for the following purposes:

Key Use Cases

  • Checking Command Location: Verifies where the executable file of the specified command is located among the paths defined in the `PATH` environment variable.
  • Version Management: Determines which version of a program will be executed in the current shell environment when multiple versions with the same name are installed.
  • Checking Program Existence in Scripts: In shell scripts, checks whether a specific program is installed, prompting for installation or handling errors if it is not.
  • Debugging the `PATH` Environment Variable: Checks whether the `PATH` settings are correct or if a specific path is included in the `PATH` so that the command can be found correctly.

Understanding the `PATH` Environment Variable

To understand how the `which` command operates, it is essential to understand the `PATH` environment variable. The `PATH` is an environment variable that stores a colon (`:`) separated list of directories where the shell searches for executable files when executing commands. The shell looks for commands in the directories listed in the order specified in the `PATH`.

Checking the `PATH` Environment Variable

echo $PATH

You can check the value of the `PATH` environment variable set in the current shell using the `echo` command.

Main Options for the 'which' Command

The `which` command is simple, but it provides useful options such as finding all matches or ignoring aliases and functions.

1. Basic Search

2. Search Control Options

Generated command:

Try combining the commands.

Description:

`which` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Learn how to efficiently determine the location of executable commands on your system through various examples of using the `which` command.

Finding the Executable Location of the `ls` Command

which ls

Outputs the absolute path of where the `ls` command is actually located (typically `/usr/bin/ls`).

Finding All Executable Locations of the `python` Command

which -a python

If there are multiple versions of the `python` executable installed on the system (e.g., `python2`, `python3`), it outputs all matching paths in the `PATH`.

Checking Command Existence (Using Scripts)

if which grep > /dev/null; then
  echo "The grep command is installed."
else
  echo "Cannot find the grep command. Installation is needed."
fi

An example of checking whether the `grep` command is installed on the system in a shell script and performing other actions accordingly.

Finding the Actual Binary Location of the `sudo` Command

which --skip-alias --skip-functions sudo

`sudo` can often be defined as an alias or shell function, but you can use `--skip-alias` and `--skip-functions` to find the path of the actual executable file.

Searching for a Non-existent Command

which nonexistent_command

When searching for a `nonexistent_command` that is not installed on the system with `which`, nothing will be output. Error messages can be viewed by redirecting with `2>&1`.


Same category commands