Discover the usage of the "whereis" command in Linux and BSD, complete with examples

Discover the usage of the "whereis" command in Linux and BSD, complete with examples

12.12.2023
Author: HostZealot Team
2 min.
807

​Before getting into the practical examples, it is essential to understand the "whereis" command and why it is useful. The "whereis" command in Linux is used to find binary files, source code, and manual pages for a command. It searches for files in a directory, which makes it faster than other commands such as "find."

Understanding the "whereis" Command

The "whereis" command is a search tool specifically designed to find binary, source, and manual page files. Unlike general-purpose search tools, "whereis" works with a predefined set of directories commonly used to store command files. This characteristic gives "whereis" the advantage in speed and accuracy.

Understanding the location of command files is helpful in the following scenarios:

  • Troubleshooting. Knowing the location of a command's binary or source files can help identify the problems with that command.
  • Scripting. When writing scripts, especially those designed to run on different systems, it is essential to specify the correct path to the command.
  • System Administration. Administrators often need to check if certain utilities or their versions are installed. The location of a program can sometimes identify its version or installation method.

Although specific commands such as "find" offer more extensive search capabilities, they scan the entire file system (or a specified directory). To compare, "whereis" in Linux limits the search to a predefined list of directories known to contain command executables, source files, and man pages. This approach significantly reduces search time, especially on large file systems.

How to Use "whereis" in Linux

The main syntax of the command in Linux is:

whereis [options] file_name

The file name section in the syntax should contain the file you want to search. For example, to find the path to binary executable files, source code, and man pages, you can add the "ls" command to the syntax:

whereis ls

The result of the command above can look something like this:

ls: /bin/ls /usr/share/man/man1/ls.1.gz

Finding the Location of a Binary

If you need to locate a Linux binary, you can use the "-b" command:

whereis -b whereis 
whereis: /usr/bin/whereis /usr/bin/X11/whereis

The mentioned command locates the binary of the "whereis" command and points out the direction of where the command is available within the Linux system.

Searching for Man Pages

For locating the man pages, the "-m" command is used:

whereis -m whereis 
whereis: /usr/share/man/man1/whereis.1.gz

It works similarly to locating a binary and gives you a path to a man page within the Linux system.

Examples of Using "whereis" in BSD Systems

1. Commands in FreeBSD

With whereis, you can also search for multiple arguments at a time. Here's an example for bash and nano:

whereis bash nano 
bash: /usr/bin/bash /usr/share/man/man1/bash.1.gz /usr/share/info/bash.info.gz 
nano: /usr/bin/nano /usr/share/nano /usr/share/man/man1/nano.1.gz /usr/share/info/nano.info.gz

In the example of nano, you can look for binaries specifically like this:

whereis -b nano nano: /usr/bin/nano /usr/share/nano

And for manual nano pages:

whereis -m nano nano: /usr/share/man/man1/nano.1.gz /usr/share/info/nano.info.gz

Optionally, you can search for several things at once, for instance firefox and nano binary, and manual bash pages:

whereis -bm nano firefox -m bash 
nano: /usr/bin/nano /usr/share/nano /usr/share/man/man1/nano.1.gz /usr/share/info/nano.info.gz firefox-m: 
bash: /usr/bin/bash /usr/share/man/man1/bash.1.gz /usr/share/info/bash.info.gz

2. Libraries in OpenBSD

Let's say you want to see all the locations. The "-l" option will give you all paths like this:

whereis -l 
bin: /usr/bin 
bin: /usr/sbin 
bin: /usr/lib 
bin: /usr/lib64 
bin: /etc bin: /usr/games 
bin: /usr/local/bin 
bin: /usr/local/sbin 
bin: /usr/local/etc 
bin: /usr/local/lib 
bin: /usr/local/game

So, the "whereis" command gives you multiple path matches, including libraries.

Tips and Tricks for Effective Use of "whereis"

Here’s a list of "whereis" options that you can use for quick application: 

-b
Binaries
-s
Sources
-m
Manuals
-u
Unusual entries. (if the command has just one entry of the requested type).
-B (list)
Limit the search places for binaries.
-S (list)
Limit the search places for sources.
-M (list)
Limit the search places for manuals and documentation.
-f
Searches for the filenames. (Should be used with any -B, -S, or -M options).
-l
Lists all the search paths.
Related Articles