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.
Table of Contents
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 ...
ShellScriptFor example, if you want to find the location of the program python
, you can enter the following command:
where python
ShellScriptWhen you run this command, it will output the path to the installed python executable file, as shown in the image below.
Additionally, if you enter multiple commands as shown below, it will output the paths for all the commands simultaneously.
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
ShellScriptThis 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.
-p Option: Path Output
The -p
option is used to output the path of the command file.
where -p name
ShellScriptEarlier, we confirmed that there are two commands named which. The -p
option will show the paths for all these commands.
-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*"
ShellScriptThe 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.
-s Option: Symbolic Links
If a command is a symbolic link, this option will output the path to the original file as well.
where -s name
ShellScriptWhen using the -s
option as shown below, the original file is displayed to the right of the symbolic link arrow (->
).
-S Option: All Paths of Symbolic Links
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
ShellScriptAs shown below, you can check the detailed path of the symbolic link.
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.