How to Use the whereis Command in Linux
The whereis Command in Linux locates binary, source, and manual files for a specified command by querying the system’s indexed database. It enables quick access to executable paths and documentation for efficient system management.
Linux whereis
command basic syntax is as follows:
whereis [options] [command_name or filename]
🤖AI Overview:
The whereis command in Linux efficiently locates the binary, source, and manual page files for a given command or file by searching the system’s pre-indexed database. It aids users in quickly finding essential files without requiring root privileges, making it an essential tool for system administration and troubleshooting.
Prerequisites to Use whereis Command in Linux (Ubuntu, CentOS, etc)
Consider the below options to use the whereis command correctly without encountering any error:
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
Option | Function |
---|---|
-b | Searches only for the location of binary files (executable file). |
-s | Prints the path to source files. |
-m | Finds 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. |
-u | Prints unusual entries that don't have the binary, source, or manual page |
-f | Terminates directory argument list. (It is often used with -B,-M and -S ) |
-l | Searches the directories the whereis command relies on |
-p | Searches only for the location of command binary. |
-V | Shows version information about whereis command. |
-h | Displays 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 an essential tool for developers and system administrators needing precise file locations associated with commands.
Its ability to efficiently identify the binary executables, source codes, and manual pages supports streamlined troubleshooting, environment setup, and automation within Linux systems.
Mastering the whereis command options and combining it with other Linux utilities enhances its utility, empowering developers to maintain better control over system resources and software management.
Regularly updating the locate database and understanding its role in whereis functionality further ensures reliability and accuracy, making the whereis Command in Linux an indispensable element of a developer’s toolkit.
FAQ
2. How do you use the basic syntax of the whereis command in Linux?
The basic syntax is: "whereis [options] [command_name or filename]".
This command searches predefined directories for binaries, source files, and manual pages related to the specified command.
3. Can whereis command locate multiple commands at once in Linux?
Yes. You can search for multiple commands by listing them separated by spaces. For example, whereis ls cat less will return the locations of the ls, cat, and less command files simultaneously.
4. What options are available with the whereis command to filter search results?
- -b locates only binary (executable) files.
- -m locates the manual page files.
- -s locates the source code files.
- -u finds unusual entries without usual binaries, source, or manual files.
You can combine these options for tailored output.
5. How can the search paths be customized with the whereis command in Linux?
Use the -B, -M, and -S options to define custom directories for binaries, manual pages, and source files respectively. Combine them with -f [command_name] to restrict the search to specified paths.
6. What are common errors when using the whereis command and how can they be fixed?
Common errors include "command not found," which can be fixed by installing the util-linux package, no output due to missing files, or permission issues requiring sudo privileges or permission adjustments.
7. How does the whereis command differ from the which command in Linux?
The which command only locates the executable binary file of a command, while whereis finds the executable, source code, and manual pages, providing more comprehensive file locations.
8. Is root access required to run the whereis command in Linux?
No root or sudo privileges are required. The whereis command can be used by any regular user to locate files related to commands.
9. How does the whereis command enhance Linux system administration?
By quickly locating binaries, sources, and manuals, whereis aids in verifying installations, debugging, customizing applications, and efficient system troubleshooting.
10. How can whereis be combined with other Linux commands for advanced usage?
For example, you can pipe whereis output to xargs to execute commands located with whereis, or combine options to retrieve multiple file locations at once for tasks like running or listing files in detail.