whereis Command in Linux

whereis Command in Linux

The whereis command in Linux is a powerful command line tool to locate the binary, source, and manual page files for a target command or file. The whereis command queries the /etc/locate.db database that contains a pre-indexed list of directories and their path on the Linux system.

Linux whereis command basic syntax is as follows:

whereis [options] [command_name or filename]

The whereis command in Linux helps find specific command’s related files location (binary (executable), its source code, its documentation files) quickly.

Mastering the whereis command in a Linux system, especially in a Linux VPS environment, has valuable advantages for Linux system administrators for efficient management, troubleshooting, and customizing applications.

Prerequisites to Use whereis Command in Linux (Ubuntu, CentOS, etc)

Consider the below options to use the whereis command correctly without encountering any error:

  • A Linux VPS running Ubuntu, Debian, CentOS, etc.
  • Access to a terminal or command-line interface.

Note: To run the Linux whereis command, you do not require root access with sudo privileges, it can be used by any regular user.

Linux whereis Command Options

OptionFunction
-bSearches only for the location of binary files (executable file).
-sPrints the path to source files.
-mFinds only manual page locations.
-B [/path/to/dir]Limits the locations for search for binary files.
-S [/path/to/dir]Defines source path for search.
-M [/path/to/dir]Defines the location where whereis searches for manuals.
-uPrints unusual entries that don't have the binary, source, or manual page
-fTerminates directory argument list. (It is often used with -B,-M and -S )
-lSearches the directories the whereis command relies on
-pSearches only for the location of command binary.
-VShows version information about whereis command.
-hDisplays help messages.

9 practical examples to understand whereis command in Linux

The Linux whereis command examples would be helpful to understand better how to use the whereis command and allow you to unlock its potential power for many use cases on Linux systems.

1. Locate your target command

The whereis command in Linux helps to find the location of a command and access documentation for given commands to have insight into where the command is running and its source codes. For example, find the grep command location by running:

whereis grep

Output:

grep: /bin/grep /usr/share/man/man1/grep.1

Explain the output:

  • /bin/grep: Indicates path to the binary (executable) file for grep.
  • /usr/share/man/man1/grep.1: Indicates the path to the manual page for grep.

Therefore, whereis command in Linux prints path/ source path/ manual page file for grep command.

Not: The whereis command returns only the command name, which means the command does not exist on your Linux system.

2. Find the whereis command

If you are curious about the predefined directory where the Linux whereis command searches for paths of the command-related files by default, you can use the -l  option with the whereis command in Linux:

whereis -l

Output:

bin: /usr/bin
bin: /usr/sbin
bin: /usr/libx86_64 linux-gnu
bin: /etc
bin: /usr/lib
bin: /usr/lib32
bin: /usr/local/bin
bin: /usr/local/sbin
bin: /usr/local/lib
bin: /usr/local
bin: /usr/libexec

3. Find the multiple commands

The whereis command in Linux allows you to look for the path to the multiple commands related files at once by typing each name separated with space. For example, to search the location of less , cat and ls command run the following command:

whereis ls cat less

Output:

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

This command saves your time to gain information about many commands by typing them one by one.

4. Find the command’s binary files

You can filter the output of the whereis command to display only binary files of the given command with the below syntax:

whereis -b [command_name]

For example, search for only the path to the binary files of the ping command with:

whereis -b ping

Output:

ping: /usr/bin/ping

5. Search for the command’s manual page location

You can search for only the manual page location of the target command using the –m option:

whereis -m [command_name]

For example:

whereis -m whoami

Output:

whoami: /usr/share/man/man1/whoami.1.gz

Therefore, it returns only the path to the man page of the ls command.

6. Locate only source code files

The Linux whereis command allows you to customize its output to print only the location of the source code files which is related to your target command. To do this use the -s option with whereis command:

whereis -s bash

Output:

bash: /etc/bash.bashrc

If the source files exist on your Linux system, this command will output their locations.

7. Search for unusual entries

Using the -u option with whereis command, search for unusual entries, files, or commands that have no expected binary, manual, or source files. To do this run the following command:

whereis -u *

This command displays all unusual entries in the current working directory.

8. Define Search Path

The Linux whereis command accepts changing the default location where it searches for the command’s document. You can specify the valid path to binaries, sources, and manual page files using the -B , -S , and -M options while using whereis command. The basic syntax is as follows:

whereis -B [binary_path] -M [manual_path] -S [source_path] -f [command_name]

You can specify a valid path after the desired option with a “/“.

For example, to limit the places where whereis searches for only binary files of the cp command in the /bin directory, run the following command:

whereis -b -B /bin -f cp

Output:

cp: /bin/cp

The -f option usually is used with -B , -S , and -M options and terminates the directory argument and indicates the use of the filenames.

If this command prints empty results, this is because your target command does not have any document in the/bin directory.

9. Combining whereis command with other Commands in Linux

The Linux whereis command can be combined with other command line tools for effective system management and file handling tasks.

Example 1: with the whereis and ping combination you can find the binary location of the ping command and then directly execute it to ping Google four times:

whereis -b ping | xargs ping -c 4 google.com

Example2: you can check the binary and manual page location for ls and tr commands in Linux and gain the only manual page for gcc command with:

whereis -bm ls tr -m gcc

Output:

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

Example3: Get details about the location of files found by the whereis command with a combination of whereis with xargs and ls commands:

whereis ls | xargs ls -l

What is the use of Linux whereis command?

You can use the Linux whereis command for the below purposes:

  • Quickly access the exact location of binary, source, and manual page files of specific commands or files
  • Finding where executables are installed for system troubleshooting
  • Verifying installations by checking installation paths and locating missing files
  • Customizing or debugging applications by accessing source files
  • Modifying and reviewing commands by locating the source code

What are common errors that might happen when using whereis command?

  • Error: whereis: command not found

Troubleshooting: If the whereis command is not available on your Linux system, you may encounter with this error which is rare for modern Linux distributions. However, to fix this error you must install the util-Linux package, which includes whereis  using the below commands:

sudo apt-get install util-linux  # For Debian/Ubuntu
sudo yum install util-linux      # For RHEL/CentOS
  • Error: no output after running whereis [command]

Troubleshooting: This error happens because the related files for the command do not exist in the default search paths. To solve this error first ensure the command is installed and use the find command or locate command as alternatives.

  • Error: Unable to find files due to restricted permissions.

Troubleshooting: This is because of not having read permissions for certain directories. To fix this issue you must modify file or directory permissions with chmod command in Linux and run the whereis command with sudo privileges.

  • Error: whereis does not find the given directories

Troubleshooting: This is happens because of non-standard directories which not covered by whereis. To solve this error you can specify search path when using whereis command.

What is the difference between whereis and which in Linux?

The which command in Linux is used to search for the path to the executable files, however, the Linux whereis command locates executable, source code, and manual page of a commands.

Conclusion

The whereis command in Linux is primarily used for finding the location of the executable files, source code, and manual pages, therefore it can not cover other file types. You can use the power of the whereis command in Linux for optimal Linux VPS management and maintenance.

Also, you can combine the whereis command options to tailor the output based on your needs.

Since whereis command relies on /etc/locate.db database, you must ensure the database is up to date before running the whereis command to gain correct information. You can update the /etc/locate.db database manually by running the updatedb command.

If you need more information about Linux whereis command, refer to its man page using the  man whereis  command.

Leave a Reply

Your email address will not be published. Required fields are marked.