which Command: Tutorial & Examples
Display the path of a command
The which
command in Linux is a command-line utility that is used to locate the executable file associated with a given command. The basic syntax for the command
is which [command]
.
When you run the which command followed by a command name, it will search through the directories specified in the PATH
environment variable for an executable file with the same
name as the command and will print the path of the executable file if it is found.
For example, if you run the command which ls
, it will return the location of the executable file for the ls
command, for example /bin/ls
.
The which
command is useful when you want to find the location of an executable file for a command, or if you have multiple versions of a command installed and you want to know
which version will be executed when you run the command. It will also tell you when you have defined an alias
that matches your search.
It is important to note that the which
command only searches through the directories specified in the PATH
environment variable, so it may not find an executable file for a
command if it is located in a non-standard directory or if the command is not installed on the system.