How to use the command 'whereis' (with examples)

How to use the command 'whereis' (with examples)

The whereis command is used to locate the binary, source, and manual page files for a given command in Unix-like operating systems. It can be useful for finding the location of important files related to a command, such as the executable, source code, and relevant documentation.

Use case 1: Locate binary, source, and man pages for ssh

Code:

whereis ssh

Motivation: Locating a command’s binary, source, and man pages can be helpful when troubleshooting or trying to understand how a command works. In the case of ssh, knowing the location of its binary, source code, and man pages can assist in examining its behavior, debugging potential issues, or modifying its source code if needed.

Explanation: By running whereis ssh, the command will search the standard binary directories, source code directories, and man page directories to determine the locations of ssh’s binary, source, and man page files, respectively.

Example output:

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

The output shows that the binary file for ssh is located at /usr/bin/ssh, and its man page is located at /usr/share/man/man1/ssh.1.gz.

Use case 2: Locate binary and man pages for ls

Code:

whereis -bm ls

Motivation: In some cases, you may only need to find the binary and man pages of a command without looking for its source code. Limiting the search to these two types of files can be more efficient and provide a quicker result.

Explanation: The -bm option specifies that only the binary (-b) and man page (-m) files should be located. By appending ls to the command, it instructs whereis to search for the binary and man pages specific to the ls command.

Example output:

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

The output shows that the binary file for ls is located at /bin/ls, and its man page is located at /usr/share/man/man1/ls.1.gz.

Use case 3: Locate source of gcc and man pages for Git

Code:

whereis -s gcc -m git

Motivation: Sometimes, you may be interested in finding multiple commands’ related files simultaneously. This use case demonstrates how to locate the source code and man pages of different commands in a single command.

Explanation: The -s option specifies that the source files (-s) should be located, and the -m option specifies that only the man pages (-m) should be located. By providing gcc and git as arguments to the command, it instructs whereis to search for the source code of gcc and the man pages of git.

Example output:

gcc: /usr/src/gcc
git: /usr/share/man/man1/git.1.gz

The output shows that the source code directory for gcc is located at /usr/src/gcc, and the man page for git is located at /usr/share/man/man1/git.1.gz.

Use case 4: Locate binaries for gcc in /usr/bin/ only

Code:

whereis -b -B /usr/bin/ -f gcc

Motivation: In certain scenarios, you may want to limit the search for a binary to a specific directory. This use case demonstrates how to locate the binary for a command in a particular directory rather than searching in all the standard binary directories.

Explanation: The -b option specifies that only the binaries (-b) should be located. The -B option is used to specify the search path, in this case, /usr/bin/. Finally, the -f option is used to locate a specific file, which, in this case, is gcc.

Example output:

gcc: /usr/bin/gcc

The output shows that the binary file for gcc is located at /usr/bin/gcc within the specified search path.

Use case 5: Locate unusual binaries

Code:

whereis -u *

Motivation: Sometimes, you may be interested in finding the binaries that have more or less than one occurrence on the system. This use case demonstrates how to locate such binaries.

Explanation: The -u option specifies that only unusual binaries (-u) should be located. By appending * to the command, it instructs whereis to search for all binaries on the system.

Example output:

unusual_bin_1: /path/to/unusual_bin_1
unusual_bin_2: /path/to/unusual_bin_2

The output shows the names and locations of the unusual binaries found on the system.

Use case 6: Locate binaries that have unusual manual entries

Code:

whereis -u -m *

Motivation: Similarly to the previous use case, you may also want to find the binaries that have an abnormal number of manual pages installed.

Explanation: The -u option specifies that only unusual binaries (-u) should be located, and the -m option specifies that the manual pages (-m) should be included in the search. By appending * to the command, it instructs whereis to search for all binaries on the system.

Example output:

unusual_bin_1: /path/to/man1/unusual_bin_1.1.gz /path/to/man2/unusual_bin_1.2.gz
unusual_bin_2: /path/to/man1/unusual_bin_2.1.gz

The output shows the names and locations of the unusual binaries that have more or less than one manual page installed.

Conclusion:

The whereis command is a useful tool for locating the binary, source, and manual page files associated with a command. By using the provided options, you can tailor the search parameters to fit your specific needs, ensuring that you find the relevant files efficiently and effectively.

Related Posts

How to use the command ‘jmap’ (with examples)

How to use the command ‘jmap’ (with examples)

The ‘jmap’ command is a tool provided by Java that allows you to obtain memory-related information for a Java process.

Read More
How to use the command `bindkey` (with examples)

How to use the command `bindkey` (with examples)

The bindkey command in Z-Shell is used to add keybindings, or shortcuts, to the shell.

Read More
How to use the command 'az webapp' (with examples)

How to use the command 'az webapp' (with examples)

Azure Cloud Services provides a powerful platform for hosting web applications in the cloud.

Read More