whereis - Linux


Overview

The whereis command in Linux is used to locate the binary, source, and manual page files for a specified command. This tool is essential for quickly finding important file paths related to commands, making it particularly useful for system administrators and developers who need to manage or compile software from source.

Syntax

The basic syntax of the whereis command is as follows:

whereis [options] <filename>

<filename> is the name of the command you want to search for. It is the only required argument in this command.

Options/Flags

  • -b: Limit the search to binary files.
  • -B <path>: Limit the search for binary files to the specified path. This should be used with -b.
  • -m: Limit the search to manual sections.
  • -M <path>: Limit the search for manual sections to the specified path. This should be used with -m.
  • -s: Limit the search to source files.
  • -S <path>: Limit the search for source files to the specified path. This should be used with -s.
  • -u: Search for unusual entries. A binary is considered unusual if it does not have one or more of the associated files (source or manual page).

Examples

  1. Find all associated files for the gcc command:

    whereis gcc
    

    This command will display the paths of the binary, source, and manual files for gcc.

  2. Search only for the binary file of ls:

    whereis -b ls
    

    This will output the path to the binary file of ls.

  3. Specify a custom path to search for binary files of nginx:

    whereis -b -B /usr/local/bin -f nginx
    

    This restricts the search for the binary file of nginx within /usr/local/bin.

Common Issues

  • No results found: This typically happens if the specified command doesn’t exist or the wrong path is given in -B, -M, or -S options. Check the command spelling and path accuracy.

  • Too many irrelevant results: This can happen when broad paths are specified. Narrow down the search path or specify the exact type of files (binary, source, manual) you’re looking for.

Integration

The whereis command can be combined with other commands for script writing or command chaining. For example, you could use the output of whereis to check if a specific program is installed and then take action based on the result:

#!/bin/bash
program="node"
location=$(whereis -b $program | cut -d ' ' -f2)
if [ -z "$location" ]; then
    echo "$program is not installed."
else
    echo "$program is installed at $location"
fi

This script checks whether the node binary is installed and reports its location.

  • which: Shows the full path of shell commands.
  • locate: Finds files by name, searching a database of indexed files.

For further detail and depth, users should consult the official man pages accessible via man whereis in the terminal or visit online resources that provide Linux command references.