Linux Command whereis and Its 7 Options

Linux command whereis helps locate where a specific file resides on the system, including related binary files (executable files), source files, and manual pages. In this post, we will explore the basic usage of the whereis command and several useful options.

What Is the Linux Command whereis?

The whereis command is primarily used to locate the executable file, source code, and manual (man) pages for a specific command. It helps to quickly find the file path of a program installed on the system, which is particularly useful when you need to verify where a program is installed. For example, to find the location of the ls command, you can enter whereis ls.

Basic Usage

whereis [option] [filename to search]
ShellScript

In the above format, the [option] can be omitted, and the [filename to search] is where you input the command or file name you are looking for.

Example

whereis ls
ShellScript

When you run this command, it will output the paths for the binary file, source file, and manual page related to the ls command. As seen below, this means that the executable file for the ls command is located at /bin/ls and the related manual page is at /usr/share/man/man1/ls.1.gz.

Figure 1. Execution result of the Linux command whereis ls
Figure 1. Execution result of the Linux command whereis ls

Key Options for the whereis Command

The whereis command offers a variety of options to help users search for files in the way they need. Let’s take a look at some of the key options.

-b: Search Only for Binary Files

The -b option searches only for binary files, excluding source files and manual pages. For example, the following command will output only the location of the ls command’s executable file:

whereis -b ls
ShellScript

In the figure below, you can see that only the path for the executable file is displayed.

Figure 2. Searching only for binary files with the -b option of the Linux command whereis
Figure 2. Searching only for binary files with the -b option of the Linux command whereis

-m: Search Only for Manual Files

The -m option searches only for manual files. For example, the following command will output only the location of the ls command’s manual file:

whereis -m ls
ShellScript

In the figure below, you can see that only the path for the manual file is displayed.

Figure 3. Searching only for manual files with the -m option of the Linux command whereis
Figure 3. Searching only for manual files with the -m option of the Linux command whereis

-s: Search Only for Source Files

The -s option searches only for source files. For example, the following command will output only the location of the ls command’s source file:

whereis -s ls
ShellScript

If the source file cannot be found, the output will simply show the command name.

Figure 4. Searching only for source files with the -s option of the Linux command whereis
Figure 4. Searching only for source files with the -s option of the Linux command whereis

-u: Output Only When a File Is Missing

The -u option outputs results only if any of the binary, source, or manual files are missing. For example, if a program lacks a manual, this option will quickly let you know.

whereis -u ls
ShellScript

-B, -M, -S: Specify Search Paths

The whereis command searches in predefined paths by default. However, if you want to search for files only in specific directories, you can use the -B, -M, or -S options to specify search paths.

  • -B: Specify the path to search for binary files
  • -M: Specify the path to search for manual files
  • -S: Specify the path to search for source files

For example, if you only want to search for the binary file of the zsh command in the /usr/bin directory, you can enter:

whereis -B /usr/bin -b zsh
ShellScript

In the figure below, you can see that there are multiple paths for zsh. If you specify the binary path as /usr/bin, it will search for the binary only in that path.

Figure 5. Specifying the search path with the Linux command whereis
Figure 5. Specifying the search path with the Linux command whereis

Tips for Using the whereis Command

Solving System Issues

The whereis command is useful when a program is not located in the expected path. It can help you quickly verify if a program is installed or diagnose issues when a program is installed in the wrong path.

Verifying Missing Manual Pages

In Linux, you can easily check the usage of commands via their manual pages. By using the whereis -m option, you can verify if a command’s manual page is missing. If it is absent, you may need to install it separately.

Checking Multiple File Locations

If you want to find the location of multiple files or commands at once, you can list the file names separated by spaces.

whereis ls cp mv
ShellScript

This command will output the file locations for ls, cp, and mv all at once, as shown below.

Figure 6. Checking the locations of multiple files with the Linux command whereis
Figure 6. Checking the locations of multiple files with the Linux command whereis

Usage Precautions

  • Be Aware of Symbolic Links: The whereis command does not follow symbolic links (links that point to other files). Therefore, it may return the location of the link rather than the actual file.
  • Path and Environment Variables: The whereis command searches based on paths defined in the PATH environment variable. If a program is installed in a directory not included in PATH, the whereis command may not be able to locate it.
  • Use Exact File Names: Be sure to input the exact file name when using whereis. Otherwise, you may get unexpected results.

Summary

Searching for files in Linux is an essential task, and the Linux command whereis is a valuable tool that quickly and accurately locates files. By leveraging various options, you can search for only the files you need or specify directories to search in. This tool is particularly helpful for verifying if programs are installed correctly or if the necessary manual pages are available.

References

Related Posts

Leave a Comment