How to Use the Linux Command where and Its 5 Options

The Linux Command where is especially useful when you need to find the location of various commands and programs. When multiple versions of software are installed, where can be a big help in determining which directory a specific program is located in. In this post, we will explore how to use the Linux Command where and its options.

What is the Linux Command where?

The Linux Command where helps users locate the position of a program or command they intend to execute on the system. This command primarily searches for the program in the paths set in the user environment variable (PATH).

Additionally, the Linux Command where is a Zsh built-in command, meaning it can only be used in Zsh and is not available in Bash. In Bash, you need to use commands like which, type, or command -v. If you’re curious, check out the related posts on How to Use the Linux Command which and Its 2 Options and How to Use the Linux Command type and Its 4 Options.

Basic Usage

The basic usage of the where command is straightforward. You simply enter the name of the program or command you want to find after the command.

where name ...
ShellScript

For example, if you want to find the location of the program python, you can enter the following command:

where python
ShellScript

When you run this command, it will output the path to the installed python executable file, as shown in the image below.

Figure 1. Linux Command where: Path to the python executable file
Figure 1. Linux Command where: Path to the python executable file

Additionally, if you enter multiple commands as shown below, it will output the paths for all the commands simultaneously.

Figure 2. Linux Command where: Search for multiple commands at once
Figure 2. Linux Command where: Search for multiple commands at once

Useful Options for the where Command

The where command in Linux provides various options. Utilizing these options can help you get more appropriate search results for specific situations. The main options are as follows:

-w Option: Command Type

The -w option shows what type of command the specified command is. It is represented as alias, builtin, command, function, hashed, reversed, or none.

where -w name
ShellScript

This option is useful when you want to check the type of a specific command. As shown in the image below, the which command is both a builtin and a command.

Figure 3. Linux Command where: Output command type with the -w option
Figure 3. Linux Command where: Output command type with the -w option

-p Option: Path Output

The -p option is used to output the path of the command file.

where -p name
ShellScript

Earlier, we confirmed that there are two commands named which. The -p option will show the paths for all these commands.

Figure 4. Linux Command where: Output the executable file path with the -p option
Figure 4. Linux Command where: Output the executable file path with the -p option

-m Option: Search by Pattern

When using the -m option, you can search for commands by pattern using wildcards. In this case, the pattern should be enclosed in quotes.

where -m "whe*"
ShellScript

The above pattern searches for all commands that start with whe. The image below shows that the shell provides the location of commands via built-in commands and paths.

Figure 5. Linux Command where: Search by pattern with the -m option
Figure 5. Linux Command where: Search by pattern with the -m option

If a command is a symbolic link, this option will output the path to the original file as well.

where -s name
ShellScript

When using the -s option as shown below, the original file is displayed to the right of the symbolic link arrow (->).

Figure 6. Linux Command where: Output the original path of a symbolic link with the -s option
Figure 6. Linux Command where: Output the original path of a symbolic link with the -s option

The uppercase -S option, similar to the lowercase -s, shows the original file path of the symbolic link but also displays all intermediate paths if there are any. This option is useful when you need to trace the order of symbolic links.

where -S name
ShellScript

As shown below, you can check the detailed path of the symbolic link.

Figure 7. Linux Command where: Output the detailed symbolic link path with the -S option
Figure 7. Linux Command where: Output the detailed symbolic link path with the -S option

Precautions When Using the Command

There are a few points to keep in mind when using the where command:

  • Only available in Zsh: The where command is a Zsh built-in command and can only be executed in Zsh. Remember, it will not run in the Bash shell.
  • Related Commands to Know: Besides where, other similar commands include which and type. The which command only outputs the first path, and the type command distinguishes whether a command is a built-in or an external command. Depending on your needs, it’s good to choose the appropriate command.

How to Utilize and Hidden Features

The where command can be used for more than just finding the location of a program. For example, when writing a script, you can check if a specific program exists and then perform different actions accordingly.

Moreover, the where command is useful when modifying the system PATH settings or managing software versions. Especially in environments where multiple versions of a program are installed, you can use where to check the path and adjust the path priority.

Summary

The Linux Command where is an incredibly useful tool for easily checking the location of programs or commands on your Linux system. By utilizing its various options, you can use it more efficiently. However, remember that it is a Zsh built-in command and can only be used in Zsh. By mastering the basic usage of commands like where, you can handle your Linux system more proficiently.

The effectiveness of your work in a Linux environment can vary significantly depending on how well you use various commands. Continue to explore the world of Linux by learning more commands.

References

Leave a Comment