[Linux] Locate the Full Path of Executable Files with the which Command

目次

Overview

The which command displays the “full path” of the file that runs when you type a command.

It is used to confirm which version of a command is being executed when multiple versions are installed. It is also helpful for identifying the exact location of a tool to use its absolute path in scripts. Note that it only searches through the directories listed in your PATH environment variable.

Specifications (Arguments and Options)

Syntax

which [options] command_name

Main Arguments and Options

OptionDescription
-aDisplays all executable files found in the search path (all). By default, it only shows the first one found.

Basic Usage

Provide the command name as an argument to see its absolute path.

Command

# Check where the python3 command is located
which python3

Execution Result

/usr/bin/python3

Practical Commands

Find All Duplicate Installations

In development environments, you might have a system version of a tool and a user-installed version (like pyenv or nvm) at the same time. Use the -a option to list all locations.

# Display all installed php commands
which -a php

Example Output:

/usr/local/bin/php
/usr/bin/php

The path displayed at the top is the one currently prioritized for execution.

Locate Administrator Commands

Some commands are not in a regular user’s PATH but are in the administrator’s (root) PATH (such as /sbin or /usr/sbin). You can search for these using sudo.

# Search for a command not found in a regular user's path
which useradd
# (No output or error)

# Search using the root's PATH via sudo
sudo which useradd

Output:

/usr/sbin/useradd

Customization Points

  • Avoiding Aliases: In some environments, which ls might show an alias (e.g., ls --color=auto). If you only want the pure path, you may need a specific option like --skip-alias (depending on the implementation) or use the type command.
  • Exit Status: The command returns 0 if it finds the command and 1 if it does not. This is useful for conditional logic in shell scripts (e.g., if which docker > /dev/null; then ...).

Important Notes

  • Only Searches the PATH: which only looks in directories registered in the PATH environment variable. It will not find files in your current directory unless that directory is in your PATH. To search the entire file system, use find or locate.
  • Shell Built-in Commands: Shell built-ins like cd or echo might not have an actual file on the disk. which might fail to find them or might point to an external version (like /usr/bin/cd). To identify the true type of a command, use the type command.
  • Distribution Differences: The implementation of which can vary between Linux distributions, so option behavior might differ slightly.

Applications

Identify the Package Belonging to a Command

You can use the path found by which to determine which package (RPM or Deb) installed that command using command substitution $(...).

For RPM-based systems (CentOS, RHEL, Fedora, etc.):

# Find which RPM package contains the httpd command
rpm -qf $(which httpd)

Output:

httpd-2.4.57-1.el9.x86_64

For Debian-based systems (Ubuntu, Debian, etc.):

# Find which deb package contains the git command
dpkg -S $(which git)

Output:

git: /usr/bin/git

Summary

The which command is a basic tool for understanding the “identity” of a command. When you see a “command not found” error, this should be the first command you run to check if the tool is missing or if its path is simply not registered. Since it may not handle shell built-ins or aliases perfectly, remember to use the type command as an alternative when needed.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次