which: Locate executable
August 12th, 2024 10:44 AM Mr. Q Categories: Command
The which command in Linux is used to locate the executable file associated with a given command in your system’s PATH. It returns the path of the executable that would have been executed if the command had been entered at the command line.
Syntax
which [options] command_name
Example Usage
- Basic Example:
which ls
Output:
/bin/ls
- This indicates that the
ls
command is located in the/bin
directory.
- Using with Multiple Commands:
which ls cp mv
Output:
/bin/ls
/bin/cp
/bin/mv
- This displays the paths of
ls
,cp
, andmv
commands.
- Show All Instances with
-a
:
which -a python
Output:
/usr/bin/python
/usr/local/bin/python
- The
-a
option shows all instances ofpython
found in directories listed in PATH, not just the first one.
Options
-a
: Lists all instances of executables found in PATH, not just the first one.-s
: Suppresses output; the command exits with status0
if any executables are found and1
if none are found.
Use Case
The which
command is helpful when you need to verify the location of a command, especially if multiple versions are installed on your system, or if you want to ensure that you are using the correct version of a command.