whereis Command in Linux | Explained

In Linux, command-line utilities play a significant role in performing different operations. These operations include system updating, installing, or removing packages, creating files, checking files/directory paths, and much more. 

These utilities are of two types “built-in” or external and each of them performs the tasks based on their specified name and functionalities. By considering its importance, the “whereis” command searches for all the possible paths of the given argument (command) that are associated with it.

Quick Outline

  1. What is the whereis Command in Linux?
  2. How Does whereis Command Work in Linux?
  3. How to Use whereis Command in Linux?
  4. Bonus Tip: Linux “which” Command

Let’s first start with the basics of the “whereis” Linux command.

What is the whereis Command in Linux?

Linux offers a pre-installed “whereis” command that retrieves the location of binary, source code, and manual page files of the specified command. This command line utility works the same as the Linux “find” command as both can be used to find the path of the path of a specified argument. 

However, the “whereis” command provides the result more accurately as compared to the “find” command. The system administrator mostly uses it to check which directories or file contains the given argument (command or program). 

After getting the basics, let’s move on to the working of the “whereis” command.

How Does whereis Command Work in Linux?

Linux “whereis” command works on its generalized syntax which is stated here:

Syntax

whereis [options] [-BMS directory  -f] command

According to the above syntax, the “whereis” command supports:

  • A large variety of supported “[options]” to customize the default output based on the user’s requirements.
  • The “-B”, “-M”, and “-S” limit the “whereis” command searches and provides a complete and well-defined path of the directories where specified commands exist. The complete path starts from the “root(/)” directory. Moreover, this path is hard-coded and cannot be changed.
  • The “command” denotes the command whose path needs to be found via the “whereis” command.

Supported Options of “whereis” Command

The below table provides the complete list of the “whereis” command’s supported options along with their descriptions: 

OptionsDescription
-bIt searches exclusively for binary files of the specified command.
-mIt searches only for binary manual section files.
-sIt searches only for the source code files.
-uIt finds the commands that have unusual entries. In Linux, the files are unusual if they do not have one entry of the specified file(binary, source/manual).
-lIt provides all the directories where the “whereis” command searches for the specified argument.
-VIt shows the installed version of the “whereis” command line utility.
-BIt limits the whereis searches for binaries files providing a list of directories separated by the whitespace characters.
-MIt limits the whereis searches for manual section files providing a list of directories separated by the whitespace characters.
-SIt limits the whereis searches for source code files providing a list of directories separated by the whitespace characters.
-fIt terminates the searched directory list and signals the start of the command written after it. It is mostly used along with the “-BMS” options.

Files

This section lists the directory structure where the binary source code and manual section files are present:

FilesContent
/usr/share/man/*All the directories that contain manual files.
/sbin, /etc, /usr/{lib,bin,ucb,lpp}All the directories that contain binary files.
/usr/src/*All the directories that contain source code files.

Now, use the above-defined syntax of the “whereis” command.

How to Use whereis Command in Linux?

The “whereis” command is used to quickly find the locations of the specified command without searching the entire Linux system manually. Apart from its basic use, it can be utilized for getting only the binary, source code, or manual section files of the given argument(command). 

This section carries out all of its possible use cases with the help of the below-stated examples.

Example 1: Default Behaviour of whereis Command

The execution of the “whereis” command without specifying any of its supported option list the directories where the specified command exist:

whereis cat

Output

The output shows the exact path of binary and manual files of the specified command “cat”:

Now, use the “whereis” command along with its supported options.

Example 2: Use whereis Command With “-b” Flag to List Binary Files

This example uses the “-b” flag of the “whereis” command to list only the binary files of the specified command:

whereis -b cat

Output

Here, the output contains the full path of the binary file that the “cat” command uses:

Example 3: Use whereis Command With “-m” Flag to Get Manual Section Files

To list the path of only manual files of the specified command, use the “-m” flag of the “whereis” command line utility in this way:

whereis -m cat

Output

Now, the output contains only the manual file location associated with the given “cat” command:

Example 4: Use whereis Command With “-s” Flag to Find Source Code Files

The execution of the “whereis” command along with its “-s” supported option finds the location of the source code files related to the given command:

whereis -s cat

Output

In this scenario, the output is empty because there are no source code files of the “cat” command:

Example 5: Use whereis Command With the “-l” Flag to List Searched Directories

This example executes the “whereis” command along with its “-l” supported option to list all the directory paths which the “whereis” command searches: 

whereis -l cat

Output

The below output contains a complete list of the searched directories for the specified “cat” command:

Example 6: Use whereis Command With “-m” and “-u” Flags to List Unusual Entries

As discussed above, the “-u” flag provides the command that has unusual entries. In Linux, a command is considered unusual if it does not have exactly one entry of the explicitly requested type.

In this example, it is used along with the combination of “-m” and the “*(asterisk)”. The combination “-m -u *” lists all the files that have no documentation or more than one: 

whereis -m -u *

Output

The execution of the above command in the current working directory shows the unusual command names:

Example 7: Use the “whereis” Command With “-B” Flag to Limit Binaries Search

This example uses the “-B” flag to limit the “whereis” command for searching only the binary files of the specified single and multiple arguments.

Single Command

For a single command specify the “-B” flag and the places where the “whereis” command searches for the binary files of the specified argument:

whereis -B /bin -f mv

In the above command the “-f” terminates the path of the directories and starts signals to the “mv” command.

Output

Here, the output only shows the binary files of the given command placed in the “/bin” directory:

Multiple Commands

Also, multiple commands are specified after the “-f” flag to search their binary files only from the specified directory:

whereis -B /bin -f ls mv

Output

The output contains the binary files of the given arguments(commands) that are present in the specified(/bin) directory:

Example 8: Use the “whereis” Command With “-M” Flag to Limit Manual Search

To limit the search places of manual section files, use the “whereis” command along with the “-M” flag. It can be used for both single and multiple arguments(commands).

First, look at the practical demonstration of the “single” argument.

Single Command

Specify the exact directory path after the “-M” flag to search for the manual section’s files associated with the given command:

whereis -M /usr/share/man/* -f mv

Output

The output contains the manual section files of the given single argument present in the specified path: 

Multiple Commands 

The multiple commands use-case follows the same process as the single argument, but with an addition of more than one command like this:

whereis -M /usr/share/man/* -f ls mv

Output

In this scenario, the output shows both the specified commands and manual section files retrieved from the given location:

Example 9: Use the “whereis” Command With “-S” Flag to Limit Source Code Files Search 

To limit the source code files search, execute the “whereis” command along with the  “-S” flag and specify the single or multiple command line arguments.

Single Command

For a single command use-case, type the search place after the “-S” flag and then specify the targeted command right after the “-f” in this way:

whereis -S /usr/src/* -f mv

Output

The below output only shows the binary and manual section files because there is no source code file of the specified command at the given path: 

Multiple Commands

For multiple commands use-case, specify more than one argument prepending with the “whereis -S path” command like this:

whereis -S /usr/src/* -f ls mv

Output

It can be observed that now the “whereis” command searches for the source code files of both the specified arguments:

Example 10: Use the “whereis” Command With “–help” Flag to get Its Details

Like each command, the details of the “whereis” command can be easily retrieved through its help page.  For this purpose, execute the “whereis” command along with its “–help” flag:

whereis --help

Output

The output shows the complete usage of the “whereis” command including its “syntax” and supported “options” along with their purposes:

It is enough about the “whereis” command in Linux.

Bonus Tip: Linux “which” Command

Linux offers another built-in command line utility “which” to get the location of the specified command. It is the simplest command that only shows the executable files of the given argument. It is mostly used in bash scripts to specify the complete path of the command instead of just specifying its name.

For a practical demonstration of the above-defined command line utility follow our detailed guide on Linux “which Command”.

Conclusion

In Linux, the “whereis” command line utility looks for the binary, source, and manual section files of the specified commands. It looks at the directories for searching the above-stated files. These directories are not the same in all Linux distributions. 

Moreover, the user can also retrieve only one type of file at the time associated with the given command. This functionality is achieved with the help of its supported options that change its default output based on the user’s requirements. This post has practically illustrated the whereis command in Linux.