Linux which Command

Which Command In Linux

The Linux which command is used to identify the path of executable files. The path is identified by searching it in the path environment variables.

Syntax of Linux which is as follows:

$ which [filename1] [filename2].. 
Whichcpp

What is the PATH variable?

PATH is an environment variable in Linux and other Unix-like operating systems that tells the shell which directories to search in for executable files.

To view the content of the PATH variable, we use the echo command as follows:

$ echo $PATH 
Whichpath

Note: Linux is case sensitive. So make sure you write PATH in uppercase.

Here’s the quick list of commonly used which command options in Linux:

  • which <command name>: Any command name that you write will give you the path for that command
  • -a: List all the matching paths for the executable
  • man which: Get the complete manual for the which command

Find the Path of Other Commands

In this example, we’ll find the path of the ping command which is used for troubleshooting, testing, and diagnosing network connectivity issues. The Linux which command is used to find the full path of the any command.

The Syntax is as follows:

$ which ping 
Linux which command ping

Multiple Arguments for the Linux which Command

We can also use multiple arguments with Linux which at the same time. Let’s have a look at the example below:

$ which netcat uptime
Whichnetcat

Since the netcat command is not available in my system, the which command displays all the paths that it searched in. The uptime command is available in our system and we can see the full path for the uptime command displayed in the last line of the output.

Print All Matching Pathnames

As we know that the search is done from left to right. Now, what if more than one matches are found of the executables listed in the PATH variable. In this case, which command will only print the first one ( just like first come, first serve ).

To print all the paths, we use -a option with which command.

The syntax is as follows:

$ which -a touch 
Whicha

Here, it has listed only one path as only one touch executable is present in the system.

Help command option

This command is used to display all the options available with which command.

The syntax is as follows:

$ info which 
Whichinfo

Alternatively, you can use the man command in Linux to get help on this, or any other command.

Conclusion

In this tutorial, we’ve gone over all the options for which command in Linux. We hope that you now know how to use the command efficiently. If you have any questions, do let us know in the comments.