Skip to content

which Linux Command Guide

The Linux which command is used to locate a specific executable file in the user’s path. It helps users determine the exact location of a command that is being executed when entered in the terminal. By providing the full path of the executable file, users can easily identify the specific directory where the command is located. This can be useful in situations where multiple versions of the same command exist on the system, or when troubleshooting issues related to command execution. The which command searches through directories in the user’s path to find the specified command and returns the path to the first instance found.

which Syntax:

Terminal window
which [options] [command]

Options:

OptionDescription
-aShows all instances of the command
-sPrints only the path of the command

Parameters:

ParameterDescription
commandThe command to be located

which Command Samples:

Locate the Path of a Specific Command

Terminal window
which ls

Locates and displays the path of the “ls” command.

Check the Path of an Executable File

Terminal window
which python3

Checks and reveals the path to the executable file for Python 3.

Determine the Path of a Binary File

Terminal window
which gcc

Determines and shows the path to the binary file for the GNU Compiler Collection.

Find the Location of a User-Defined Alias

Terminal window
which ll

Finds and displays the location of a user-defined alias “ll”.

Search for the Path of a Shell Built-In Command

Terminal window
which cd

Searches for and shows the path of the shell built-in command “cd”.

Locate the Location of a System Utility

Terminal window
which df

Locates and reveals the location of the system utility for disk usage.

Discover the Path of a Custom Script

Terminal window
which myscript.sh

Discovers and presents the path to a custom script named “myscript.sh”.

which FAQ:

How do I use which in Linux?

To use the which command in Linux, execute the following command:

Terminal window
which --help

What is the purpose of using which in Linux?

The which command in Linux is used to locate a command and display its absolute path.

How can I find the location of a specific command using which?

You can find the location of a specific command using which by providing the command as an argument. For example:

Terminal window
which nano

Can which command help me determine if a command is available in my system?

Yes, you can use the which command to determine if a command is available in your system or to see which version of the command will be executed.

How does which handle aliases and built-in commands?

When you use which with aliases or built-in commands, it may not show the expected result as it primarily focuses on locating executable files in the system.

Is there a way to customize the output format of the which command?

Yes, you can customize the output format of the which command by using options such as -a, -s, and -V to display all matches, only the first match, and the version of which command respectively.

Applications of the which command

  • Finding the location of an executable file in the system
  • Checking the path of a specific command or program
  • Verifying whether a command or executable is available in the system or not