- How to Use the which Command in Linux
- Linux which Command Syntax and Options
- Linux which Command Examples
- 1. Display the Path of Any Executable File
- 2. Display Multiple Paths of Executable Files
- 3. List All Instances
- 4. Find Symbolic Links
- 5. Exclude Shell Built-ins
- How to find application’s path from command line?
- 5 Answers 5
- Bash script to find the path of an executable
- 2 Answers 2
How to Use the which Command in Linux
The which command allows users to search the list of paths in the $PATH environment variable and outputs the full path of the command specified as an argument. The command works by locating the executable file matching the given command.
In this tutorial, you will learn to use the which command.
Linux which Command Syntax and Options
The syntax for the which command is:
The [argument] variable specifies the command or commands you want to find.
For example, the following command outputs the location of the cat command:
The which command has only one option, -a . It is optional and used to print all the matches it finds.
The command searches for matches from left to right. If there are multiple matches found in the directories listed in $PATH , which prints only the first one. The -a option instructs which to print all the matches.
Important: On many Linux distributions, which excludes the shell built-in commands and does not output their location.
Having multiple matches sometimes means one match is a symlink to the other. However, it is possible to have two versions of the same command in different locations or two different commands using the same name.
Note: Unlike many other commands, which has no —help option. To see the command description and help, run man which .
Exit Status
The which command returns one of the following values that indicate its exit status:
- 0 . All arguments were found and executable.
- 1 . One or more arguments don’t exist or aren’t executable.
- 2 . An invalid option has been specified.
Linux which Command Examples
The following examples showcase how the which command works and how to use the available option.
1. Display the Path of Any Executable File
To display the path of any command, pass the command name as an argument after which .
The output shows the path to the tr command executable file, located in /usr/bin/tr.
2. Display Multiple Paths of Executable Files
which accepts multiple arguments and outputs the path to each one in the specified order.
The command works through the supplied list and outputs the results for the nc command, mount command, and sort command, separating each result with a newline character.
3. List All Instances
which only shows the first match it finds in the $PATH variable directory list. Use the -a option to show every match for the specified command.
For example, searching for instances of the less command outputs two results when using the -a option:
Use the ls command to check file details and determine if both versions are executable files. Run:
ls -lh /usr/bin/less ls -lh /bin/less
The output shows two identical versions of the same command in two locations, both 176 KB large, and both executable.
Note: The /bin directory contains executables that can be used by the system administrator and any other user, and which are required for emergency system repairs. The /usr/bin directory is the primary directory for executable commands on the system.
4. Find Symbolic Links
Using the -a option lists all the paths containing an instance of the specified program. While multiple versions of the same program can exist on a system, sometimes one of the instances is only a symbolic link and not a binary file.
For example, running the following command outputs two instances of the atq command:
Again, use the ls command to check the details for both files. Run:
ls -lh /usr/bin/atq ls -lh /bin/atq
The output shows that both files are symbolic links ( -> ) only 2 bytes large and pointing to the at command.
5. Exclude Shell Built-ins
As previously mentioned, the which command excludes shell built-ins from its output.
For example, asking for the location of the read and man commands only outputs the location for the man command executable file, as read is a bash shell command.
This tutorial showed how to use the which command in Linux to find the path to a command’s executable binary. See and download our Linux commands cheat sheet for other essential Linux commands and examples of using them.
How to find application’s path from command line?
For example, I have git installed on my system. But I don’t remember where I installed it, so which command is fit to find this out?
Just in case, command -v and which worked in Linux Alpine 3.16.2 (Docker image). whereis and locate did not — not installed.
5 Answers 5
If it is in your path, then you can run either type git or which git . The which command has had problems getting the proper path (confusion between environment and dot files). For type , you can get just the path with the -p argument.
If it is not in your path, then it’s best to look for it with locate -b git It will find anything named ‘git’. It’ll be a long list, so might be good to qualify it with locate -b git | fgrep -w bin .
I use locate endlessly (it is very fast), but for those unaware of it, locate is only as up to date as its most recent database update, which is automatically run daily on my Ubuntu. The refresh command is sudo updatedb . Also locate has built-in regex capability, so commands like this works: locate -br «^git$» . -b` means restrict the search to just the basename . or without the -b , it searches the full pathname .. Also, it only searches paths you have configured it to search.. there is no command-line control of this other than your regex filters.
@Gilles, that’s funny for me the behavior is exactly the opposite: type is a shell builtin that tells me aliases and such, and which is an external program that shows me the path to an executable. although if there’s a builtin that gets in the way that executable won’t get called.
@quodlibetor The problems with which are that it doesn’t know about shell built-ins and functions (which is relevant when you’re wondering what typing the command will do), and it uses a different $PATH on some systems.
Bash script to find the path of an executable
EDIT: I KNOW THIS IS REDUNDANT, IT IS HOMEWORK, I HAVE WRITTEN MY OWN CODE AND NEED HELP TROUBLESHOOTING> As stated, I must write a BASH script to determine whether an executable file is in the users path. such that if you type
./findcmd ping it returns /bin/ping I have some code written, But it does not properly work and I hope someone can help me troubleshoot. When I type ./findcmd ping it just returns my file does not exist.(with any other file I try as well that I know exists.)
#!/bin/bash # # Invoke as ./findcmd command # # Check for argument if [[ $# -ne 1 ]] then echo 'useage: ./findcmd command' exit 1 fi # # Check for one argument if [[ $# -eq 1 ]] then pathlist=`echo $PATH | tr ':' ' '` for d in $pathlist; do if [[ ! -d $d || ! -x $d || ! -r $d ]] then echo 'You do not have read end execute permissions!' exit 2 fi if [[ $(find $d -name $1 -print | wc -l) -ne 0 ]] then echo 'The file does not exist in the PATH!' exit 0 fi done fi exit 0 # #
There might be other problems too, but one is in: pathlist=`echo $PATH | tr ‘:’ ‘ ‘` . You want to create an array here: pathlist=($(echo $PATH | tr ‘:’ ‘ ‘))
When you discover a path where you don’t have rx permissions, why stop with exit 2 ? It would be better to ignore the bad paths completely and continue the loop to find one that works (if any).
Basically, your tests are almost the opposite of what they should be. you want to stop when you find a path that works, otherwise keep going until you run out.
What happens if you eliminate the rx test for now, the first if/fi block, completely, and invert the second test, i.e. exit when find succeeds ?
2 Answers 2
No need to use a bash array, tr ‘ing the ‘:’ with ‘ ‘ will work just fine in a for loop.
#!/bin/bash # # Invoke as ./findcmd command # # Check for argument if [[ $# -ne 1 ]] then echo 'usage: ./findcmd command' exit 1 fi f=$1 # No need to check the $# again, there's at least one arg and other will be ignored.. # Otherwise you can wrap this in a loop and keep shift'ing args and checking one by one pathlist=`echo $PATH | tr ':' '\n'` for d in $pathlist; do #echo command is your friend #echo "Checking for $f in $d" path="$d/$f" if [[ -f "$path" && -x "$path" ]]; then # PATH is not recursive, therefore no need to use find command # Simply checking that the file exists and is executable should be enough echo "Found $f at '$path'" # Note the same filename may be present farther down the PATH # Once the first executable is found, exit exit 0 fi done # Getting here means file was not found echo "$f could not be found" exit 1
rbanikaz@lightsaber:~$ ./which.sh grep Found grep at '/usr/bin/grep' rbanikaz@lightsaber:~$ ./which.sh foo foo could not be found rbanikaz@lightsaber:~$