Title: Linux Which Command
Excerpt: A practical tutorial with examples on using the "which" command on Linux and how to find various executables and scripts from the PATH variable.
Permalink: linux-which-command
Category:Linux commands
In this guide, we will learn about the “Which” command in Linux.
To perform the steps demonstrated in this guide, you will need the following components:
Modern Linux systems come with multiple tools built-in for various tasks such as system management, automation, system monitoring, and remote computing. The "which" command is one of them.
The main use of the "which" command is to locate the location of executable files (and scripts). Please see the example below:
$Which one is
here:
Basic usage
To find the binary executable of a command/tool, use the "Which" command as follows:
$,
The "which" command also accepts multiple parameters. Check out the following examples:
$which ls man chmod python3
here:
Find multiple executable files
In Linux systems, there can be multiple copies of the same tool path location. For example, /usr/bin, /usr/sbin, /bin, and /sbin have overlapping executables:
$ls—l/usr/bin
$ls-L/usr/sbin
$ls-L/bin
$ls—l/sbin
Although there are multiple copies, the shell only runs a specific copy of the executable (usually located under /usr/bin) whenever a command is run. By default, the "Which" command reports this location of the "default" executable.
However, we can instruct "which" to report the locations of all matching copies of an executable.
$which—Close
$which—Close chmod bash
After running the query, the "which" command will leave an exit code. The value of the code indicates whether the operation was successful.
This is a list of all exit passwords
In Bash, after running any command, the exit code is stored in a variable. To view the value, use the following command:
$which
$ECHO$?
$Which asdfg
$ECHO$?
If you plan to incorporate the "which" command into a shell script, it is also beneficial to know the exit code. If you're a beginner, check out this beginner's guide to Bash scripting.
Other documents
Most Linux tools come with detailed documentation outlining all available parameters. For more in-depth documentation, check out the man page:
$man,
Whenever any query is run, the "which" command looks for executable files in the directory specified in the PATH environment variable. In this section, we'll take a quick look at how to use PATH.
To view the contents of this variable, run the "ECHO" command as follows:
$ECHO$PATH
$ tr ‘:’ ‘\n’
The above is the detailed content of Which command in Linux. For more information, please follow other related articles on the PHP Chinese website!