Are you scuffling with the challenge of locating essential system commands within your operating system (OS)? Do not sweat it, as it is a common hurdle. But on the bright side, you can count on the whereis
command to lend you a hand.
In this tutorial, you will learn how to effectively use the whereis
command as a tool that swiftly guides you to the precise locations of those elusive commands and utilities.
Dive into the world of whereis
and take full command of your system!
Prerequisites
Before diving in, ensure you have a Linux machine (preferably Ubuntu) with a terminal emulator.
Locating a Command’s Binary File via the whereis
Command
Are you constantly dealing with aliases, symbolic links, and different versions of the same command? If yes, knowing a command’s binary file path via the whereis
command is especially helpful.
Below is the basic syntax for the whereis
command:
[options]
– Considered as optional command-line arguments, these options let you specify the type of files to search for and the locations to search in.<command_name>
– Specifies the command you wish to locate the binary file of.
whereis [options] <command_name>
To see how the whereis
command works in action, follow these steps:
Open your terminal, and execute the following whereis
command to list the number of options available.
whereis -h
The following list in your terminal shows all the options you can use with the whereis
command. Knowing these options provides you more control over how you want the whereis
command to operate.
Now, run the below whereis
command to locate the path of the ls
(or any other) command’s binary file, manual pages, and source files.
💡 Without specifying options, the
whereis
command only searches for three types of files: binaries, manuals, and source files in their default locations.
whereis ls
In the output below, you can see two paths as follows:
Path | File Type | Details |
---|---|---|
/usr/bin/ls | Binary file | Indicates the binary file for the ls command is located in the /usr/bin directory. This directory contains all the essential commands installed on your system. |
/usr/share/man/man1/ls.1.gz | Manual page | Signifies the manual page for the ls command is located in the /usr/share/man/man1 directory. This directory holds all the manual pages installed on your system. The .gz extension indicates that the manual page is currently compressed. |
💡 Remember that the exact path can vary depending on your Linux distribution, version, and system configuration.
Employing Located Commands
Now that you know the location of your chosen command, you can employ that command in various ways. One example is you can run the command directly by specifying its path. Or automate tasks in a custom script that runs the command.
To employ located commands, the ls
command, for example:
Execute the following command to run the ls
command from its specified path directly.
/usr/bin/ls
If the specified path is correct, you will see the files and folders in the working directory listed, as shown below.
Alternatively, run the below command to add the directory containing the command to your system’s PATH environment variable. Doing so allows you to execute the command from anywhere on the system.
💡 If the command is in the current directory, the shell does not search for the command in the
PATH
environment variable.
export PATH=$PATH:/usr/bin
Once added to the PATH environment variable, you can run the command (i.e., ls
) without specifying the full path, as shown below.
Filtering the Search Result by the Base Name
The whereis
command also provides several switches you can use to modify its behavior and enhance the search process. One example is the -b
or --basename
switch, which lets you avoid ambiguity by narrowing the search to the exact binary you are interested in.
Execute the following whereis
command to find the path of the ls
command, limited to its base name (-b
or --basename
).
whereis -b ls
If successful, you will see the following output. Notice that only one path is listed,/usr/bin/ls, the path to the ls
command’s binary file.
Locating Commands via a Custom Set of Directories
Explicitly defining directories where the whereis
command should search for binary executables can help improve the performance of the search process.
On that note, the B
or --path
switch can help limit the search to specific directories. This switch is beneficial when searching for commands in large filesystems or complex directory structures.
To see how the B
or --path
switch works:
Run the following command to search for the ls
command’s binary files (-f
) only in the /usr/local/bin
and /opt/bin
directories.
Notice that you specify multiple directories in the command by separating them with colons (:
) on Unix-like systems (or semicolons (;
) on Windows).
whereis -B /usr/local/bin:/opt/bin -f ls
Finding a Command’s Documentation
When working in environments with limited or no internet connectivity, knowing the paths of the manual pages is particularly valuable.
To find a command’s documentation:
Execute the following command to search for manual pages (-m
or --man
) for the ls
command, excluding binary and source code files related to the command.
whereis -m ls
Notice below that only the manual page for the ls
command is listed in the output.
Accessing a Command’s Source Code Files
Similar to explicitly finding a command’s documentation, you can search for a command’s source code files by appending the -s
or --source
switch. Accessing the source code can provide insight into how a command is implemented, helping you understand its functionality and behavior more deeply.
Being able to access a command’s source code files is especially helpful when you wish to:
- Customize or modify a command’s behavior directly.
- Diagnose issues or troubleshoot problems related to a command’s behavior.
- Contribute improvements or fixes to the project (if the command is open source) if you are familiar with the source code.
To access a command’s source code files:
Run the below whereis
command to find the source code files of the ls
command.
whereis -s ls
Why does the ls
command not have source code files? The availability of source code files for some commands, like the ls
command, depends on several factors.
These factors include the command itself, the distribution of Linux you are using, and how the distribution packages and manages its software.
Searching for Unusual Entries
Unusual entries are files that do not have exactly one entry of each requested type: binary, manual, and source. Searching for unusual entries also helps with troubleshooting error messages.
Execute the following command to search for the ls
command and identify unusual (-u
or --usage
) entries.
whereis -u ls
If the ls
command has only two entries or more than three entries, as shown below, they are considered unusual entries.
Performing Complex Searches Using Different Switches
You have seen how to use individual switches with the whereis
command, but why not use multiple switches? The whereis
command is not limited to only one switch when searching for commands.
Combining switches to perform complex searches lets you narrow your search or focus on specific types of files associated with a command.
To see how combined switches work:
Execute the following command to search for the ls
command in the /usr/bin
directory (-B
), limited to manual pages (-m
) and source code files (-s
).
whereis -B /usr/bin -m -s ls
The output excludes binary files, as shown below. But since the ls
command’s source code is not found, you can only see the path for the command’s manual page.
Conclusion
In this tutorial, you learned that the whereis
command is not just a tool but your trusty map in this terminal landscape. In the vast realm of the command line, the whereis
command emerges as a true companion.
With this newfound knowledge, you can confidently navigate the intricate paths of Linux commands, accessing their binary files, manual pages, and source code.
Now, why not delve into the world of package management (i.e., apt
for Debian/Ubuntu)? Cultivate a deeper insight into how the software ecosystem on your Linux system works!