Skip to main content

Understanding the `which` Command in Linux: Locating Command Executables

In Linux and Unix-like operating systems, it's crucial to know where an executable is located and what kind of executable you're dealing with. While the type command provides insights into the nature of a command, the which command specifically tells you which executable will be run when you type a particular command.

Syntax

The general syntax of the which command is:

which [options] command_name
  • options: Optional flags to modify the behavior of the which command.
  • command_name refers to the name of the command you are trying to locate.

Options Table

OptionShorthandDescription
--all-aPrint all matching pathnames of the given command_name
--skip-aliasNoneIgnore command aliases
--skip-dotNoneSkip directories in PATH that start with a dot
--read-aliasNoneRead list of aliases from stdin
--skip-tildeNoneSkip directories in PATH that start with a tilde
--show-dotNoneIf a command is found in the current directory, output the entry with a dot
--versionNoneDisplay the version information and exit

Examples

Let's explore some examples of using which to find out which executable a command points to.

Example 1: Basic Use Case

which ls

Output:

/usr/bin/ls

The which command tells you that the ls command corresponds to the executable located at /usr/bin/ls.

Example 2: Multiple Matches with -a

which -a python

Output (may vary based on your system):

/usr/bin/python
/usr/local/bin/python

In this example, the -a option shows all occurrences of python found in directories specified in the PATH environment variable.

Differences between which and type, and When to Use Each

Scope of Identification

  • which: Primarily focuses on finding the path of the executable file that a command points to.
  • type: Provides a more comprehensive overview, including whether the command is a built-in shell command, an alias, or a function.

Handling Built-ins, Aliases, and Functions

  • which: Generally doesn't handle built-ins, aliases, or functions. It focuses only on external executables.
  • type: Can identify built-in commands, aliases, and functions, not just external executables.

Example

Consider the alias ll set as ls -lah.

  • Running which ll would likely return nothing or a path depending on the shell and environment.
  • Running type ll would indicate that it is an alias set to ls -lah.

When to Use Which?

  • Use which when you are interested in knowing only about the executable file a command refers to.
  • Use type when you want more detailed information including the possibility that the command could be a built-in, an alias, or a shell function.

Conclusion

While both which and type offer ways to explore command attributes, they serve slightly different purposes. The which command is your go-to option for quickly finding out which executable file a command points to, especially if you expect the command to be an external program. On the other hand, type provides more detailed insights and is better suited for a broader range of command types, including built-ins, aliases, and functions. Choose the tool that best suits your specific needs.

What Can You DO Next 🙏😊

If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.

YouTube @cloudaffle