Linux whereis Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the whereis command in Linux to locate executable files, source code, and manual pages for various programs on your system. The whereis command is a useful tool for quickly finding the location of a command or program, as it searches a predefined list of standard binary directories. You will also learn how to customize the search behavior of the whereis command to suit your specific needs.

The lab covers the following steps:

  1. Understand the Purpose of the whereis Command
  2. Locate Executable Files, Source Code, and Manual Pages Using whereis
  3. Customize the Search Behavior of the whereis Command

The whereis command is a powerful tool for Linux users, and this lab will provide you with practical examples and insights to help you effectively utilize it in your daily workflow.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux/FileandDirectoryManagementGroup -.-> linux/locate("`File Locating`") linux/FileandDirectoryManagementGroup -.-> linux/which("`Command Locating`") linux/FileandDirectoryManagementGroup -.-> linux/whereis("`File/Command Finding`") subgraph Lab Skills linux/locate -.-> lab-423006{{"`Linux whereis Command with Practical Examples`"}} linux/which -.-> lab-423006{{"`Linux whereis Command with Practical Examples`"}} linux/whereis -.-> lab-423006{{"`Linux whereis Command with Practical Examples`"}} end

Understand the Purpose of the whereis Command

In this step, you will learn about the purpose and usage of the whereis command in Linux. The whereis command is a useful tool for locating the executable, source, and manual pages for a given command or program.

The whereis command searches for a program in a predefined list of standard binary directories, such as /bin, /usr/bin, and /usr/sbin. It can help you quickly find the location of a command or program on your system.

Let's start by running the whereis command to locate the ls command:

whereis ls

Example output:

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

The output shows that the executable file for the ls command is located in the /usr/bin/ls directory, and the corresponding manual page is located in the /usr/share/man/man1/ls.1.gz file.

The whereis command can also be used to locate the source code for a program. For example, to find the source code for the gcc compiler, you can run:

whereis gcc

Example output:

gcc: /usr/bin/gcc /usr/lib/gcc /usr/include/gcc /usr/share/man/man1/gcc.1.gz

This output indicates that the executable file for gcc is located in the /usr/bin/gcc directory, and the source code and include files are located in the /usr/lib/gcc and /usr/include/gcc directories, respectively.

The whereis command is a powerful tool for quickly locating files related to a specific program or command on your Linux system.

Locate Executable Files, Source Code, and Manual Pages Using whereis

In this step, you will learn how to use the whereis command to locate executable files, source code, and manual pages for various programs on your Linux system.

Let's start by finding the location of the gcc compiler:

whereis gcc

Example output:

gcc: /usr/bin/gcc /usr/lib/gcc /usr/include/gcc /usr/share/man/man1/gcc.1.gz

This output shows that the gcc executable is located in the /usr/bin/gcc directory, the source code files are in the /usr/lib/gcc directory, and the manual page is in the /usr/share/man/man1/gcc.1.gz file.

Next, let's find the location of the python3 executable:

whereis python3

Example output:

python3: /usr/bin/python3 /usr/lib/python3.10 /usr/share/man/man1/python3.1.gz

The output indicates that the python3 executable is located in the /usr/bin/python3 directory, the Python 3.10 library files are in the /usr/lib/python3.10 directory, and the manual page is in the /usr/share/man/man1/python3.1.gz file.

You can use the whereis command to locate files related to any program or command on your system. This can be particularly useful when you need to find the location of a specific executable, source code, or manual page.

In this step, you will learn how to customize the search behavior of the whereis command to better suit your needs.

By default, the whereis command searches for programs in a predefined list of directories, such as /bin, /usr/bin, and /usr/sbin. However, you can modify the search behavior by using the -b, -m, and -s options.

Let's explore these options:

  1. -b (binaries): This option tells whereis to search only for binary executable files.
whereis -b gcc

Example output:

gcc: /usr/bin/gcc
  1. -m (manual): This option tells whereis to search only for manual pages.
whereis -m gcc

Example output:

gcc: /usr/share/man/man1/gcc.1.gz
  1. -s (source): This option tells whereis to search only for source code files.
whereis -s gcc

Example output:

gcc: /usr/lib/gcc

You can also combine these options to customize the search even further. For example, to search for the binary and manual page of the python3 command, you can use:

whereis -b -m python3

Example output:

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

By using these options, you can tailor the whereis command to your specific needs and quickly locate the files you're looking for.

Summary

In this lab, you first learned about the purpose and usage of the whereis command in Linux. The whereis command is a useful tool for locating the executable, source code, and manual pages for a given command or program. You practiced using the whereis command to find the location of the ls and gcc commands, and discovered that it can help you quickly identify the relevant files associated with a program on your system.

Next, you delved deeper into using the whereis command to locate executable files, source code, and manual pages for various programs. You explored how the whereis command searches for files in a predefined list of standard binary directories, and demonstrated its ability to provide the necessary information to find the relevant files for programs like gcc.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like