which Command in Linux with Examples

The whereis command helps you locate binaries, source trees, and documentation files for a command, which is useful for troubleshooting, scripting, and auditing system paths. This guide explains how the whereis command works, shows practical examples, and highlights options that matter on modern Linux systems.

Understand the whereis Command

Before diving into the practical aspects, it helps to know what the whereis command does and why it is useful. The whereis command in Linux locates the binary, source, and documentation files for a name. It searches a predefined set of directories, which keeps it faster than full filesystem searches.

What is whereis?

The whereis command searches for the binary, source, and manual page files for a given command. Unlike generic search tools, whereis operates on a predefined set of directories that commonly store command files. This focus gives whereis its edge in speed and accuracy.

Why is it Faster than Other Utilities?

While utilities like find offer a broader search capability, they can scan entire directory trees. In contrast, whereis restricts its search to a predefined list of directories known to contain command binaries, source files, and documentation. This focused approach reduces search time, especially on large file systems. For deep filesystem scans, use the find -exec guide.

Common whereis Use Cases

Understanding the location of command files is not just a matter of convenience. It’s often essential for:

  1. Troubleshooting: Knowing where a command’s binary or source files are can help diagnose issues related to that command.
  2. Scripting: When writing scripts, especially those meant to run across different systems, it’s crucial to reference the correct command path.
  3. System Administration: Admins often need to confirm whether a utility is installed and which copy is being resolved on the system.

Basics of the whereis Command

The fundamental structure of the whereis command is:

whereis [options] command_name

If you supply custom search paths with -B, -S, or -M, terminate the path list with -f before the command name to avoid errors.

For instance, to locate the binary, source, and documentation files for the ls command:

whereis ls

This can return output like the following on a minimal system:

ls: /usr/bin/ls

If you install documentation files, whereis includes man or info paths in the same line.

Install whereis if it is missing

Most distributions ship whereis as part of util-linux, but minimal containers or stripped images can omit it. Use the commands below to install the package for your distro.

Ubuntu and Debian-based distributions

First, refresh the APT index, then install util-linux if your system does not include it.

sudo apt update
sudo apt install util-linux

Running apt update refreshes the package index so you install the latest available release.

Fedora

On Fedora, install util-linux with DNF5.

sudo dnf install util-linux

RHEL, Rocky Linux, AlmaLinux, and CentOS Stream

On RHEL and compatible distributions, install util-linux with DNF.

sudo dnf install util-linux

Arch Linux and Manjaro

Use Pacman to install util-linux on Arch-based systems.

sudo pacman -S util-linux

openSUSE

Use Zypper to install util-linux on openSUSE.

sudo zypper install util-linux

Alpine Linux

Alpine ships the whereis binary in util-linux-misc.

sudo apk add util-linux-misc

Gentoo

On Gentoo, install sys-apps/util-linux with Portage.

sudo emerge --ask sys-apps/util-linux

Void Linux

On Void, install util-linux with XBPS.

sudo xbps-install -S util-linux

Verify availability by checking the help output. The output starts with the usage lines shown here.

whereis --help
Usage:
 whereis [options] [-BMS <dir>... -f] <name>

Locate the binary, source, and manual-page files for a command.

Options:
 -b         search only for binaries
 -B <dirs>  define binaries lookup path
 -m         search only for manuals and infos
 -M <dirs>  define man and info lookup path
...

Practical Examples of whereis

Discovering the Location of System Utilities

System utilities, such as passwd which manages user account data, are core administration tools. To determine where the binary and associated files for passwd are:

whereis passwd

Executing this might display:

passwd: /usr/bin/passwd /etc/passwd

This output shows the binary location and the /etc/passwd account database file, which stores local user entries rather than command configuration.

Locating Text Search Tools

Tools like grep search inside files, so knowing their path helps with scripts and audits. To locate grep (see the grep command guide):

whereis grep

The output may include the binary and info documentation:

grep: /usr/bin/grep /usr/share/info/grep.info.gz

Info files are part of the documentation set that whereis reports alongside binaries.

Locating Stream Editors

Stream editors like sed handle in-place transformations, and you can confirm their locations the same way. For broader usage, see the sed command guide:

whereis sed

This can return the binary and info file paths:

sed: /usr/bin/sed /usr/share/info/sed.info.gz

On systems that include man pages, you may also see man paths alongside the info file.

Finding Text Processing Commands

Commands like awk, a text processing tool, are staples in many Linux tasks. To determine where awk and its related files are:

whereis awk

This could produce:

awk: /usr/bin/awk

This output provides a clear view of the awk binary location, which is useful when scripts require an absolute path.

Locating Shells and Interpreters

Scripts often invoke shells like bash directly. To locate bash on the system:

whereis bash

The terminal might display:

bash: /usr/bin/bash

Knowing the interpreter path helps when you need an explicit shebang or a full path in automation.

Determining Locations of Compression Tools

Compression tools like tar handle archiving tasks. To locate the binary and associated files for tar:

whereis tar

Executing this might return:

tar: /usr/bin/tar

This output showcases the binary location for tar, which is helpful when scripts need the absolute path.

Locating File Management Commands

Commands like mkdir create directories, and you can confirm their paths when troubleshooting PATH issues. For more examples, see the mkdir command guide:

whereis mkdir

This might yield:

mkdir: /usr/bin/mkdir

This confirms the exact binary path your shell uses.

Locating Disk Management Utilities

Disk utilities such as lsblk list block devices and partitions. To find where lsblk is installed:

whereis lsblk

This might produce:

lsblk: /usr/bin/lsblk

This command reveals the location of the lsblk utility, which is helpful when troubleshooting storage scripts.

Locating Only Binaries

When working with Linux, sometimes you only need to know where the executable binary of a command lives, especially if you’re trying to execute it from scripts or other programs. The -b option with whereis is designed for this purpose.

whereis -b ls

Executing this command might yield:

ls: /usr/bin/ls

In this output, the -b option directs whereis to display only the binary location. On modern distributions, /bin often points to /usr/bin, so you may see /usr/bin/ls even when searching for binaries only.

Finding Only Source Files

For developers or those looking to modify or understand a command’s workings, the source files are invaluable. The -s option helps locate these files.

whereis -s ls

On most systems, this returns an empty result because most distributions do not install source files by default:

ls:

If you install source packages for a command, -s will list those locations.

Locating Manual and Info Pages

Manual pages and info pages are the built-in documentation for Linux commands. They provide detailed descriptions, options, and usage examples, and the -m option locates them.

whereis -m grep

This command might produce:

grep: /usr/share/info/grep.info.gz

Minimal images often strip man pages, so info paths might be the only documentation you see.

Locating Files for Multiple Commands

Sometimes, efficiency demands that we locate files for multiple commands in one go, especially if we’re auditing or documenting. whereis supports this by allowing multiple command names in a single invocation.

whereis ls pwd

When you run it, this command displays the locations of binaries, source files, and documentation for both the ls and pwd commands:

ls: /usr/bin/ls
pwd: /usr/bin/pwd

Excluding Certain Types of Files

There might be scenarios where you’re interested in everything about a command except a specific type of file. For instance, you might want to know about the ls command but aren’t interested in its binary. The whereis command offers the flexibility to exclude certain types of files.

whereis -ms ls

Running this command limits the results to source and documentation paths, excluding binaries. On minimal systems, you may see an empty result:

ls:

This selective search is useful when you only care about documentation or source trees.

Advanced Usage and Tips

List whereis Search Paths

The -l option prints the directories whereis searches for binaries, sources, and documentation.

whereis -l

The output starts like this:

bin: /usr/bin
bin: /usr/sbin
bin: /usr/lib/x86_64-linux-gnu
bin: /usr/lib
bin: /usr/lib64
bin: /etc
...

Understanding Absence of Output

If whereis returns only a command name and a colon, the system lacks the binary or documentation, or the files live outside the default search paths. Minimal images often strip documentation, so verify package installation before assuming the command is missing.

Using whereis with Wildcards

Wildcards can be a powerful ally with whereis when you want to search for a family of commands. On newer releases, use the -g option to interpret the name as a glob pattern:

whereis -g 'ls*'

This command might return locations for ls, lsblk, lscpu, and more, depending on what is installed:

ls*: /usr/bin/lsblk /usr/bin/lsipc /usr/bin/lscpu /usr/bin/lslocks /usr/bin/lsns /usr/bin/lsattr /usr/bin/ls /usr/lib/lsb /etc/lsb-release

Ubuntu 22.04 lacks the -g option, and whereis -g 'ls*' returns whereis: bad usage. In that case, list command names explicitly, for example whereis ls lsblk lscpu.

Limiting the Search Path

whereis searches in standard directories by default, but you can limit the search to specific paths. The -B, -S, and -M options define custom paths, and -f ends the list before the command name.

whereis -b -B /bin -f ls

This command confines the search to /bin and shows only binaries:

ls: /usr/bin/ls

On modern distributions, /bin often points to /usr/bin, so the binary still resolves there.

Search a Specific Documentation Path

You can point the documentation search to a specific directory with -M. For example, search the info directory for grep documentation:

whereis -m -M /usr/share/info -f grep

This might produce:

grep: /usr/share/info/grep.info.gz

This is useful when you want to restrict documentation results to a known path.

Search Multiple Binary Paths

whereis does not exclude directories directly, so define the paths you want to search. You can provide multiple directories after -B and end the list with -f:

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

This might produce:

grep: /usr/bin/grep

Add or remove directories as needed to focus the search.

Troubleshoot Common whereis Errors

If you see an error about a missing -f option, you did not separate the command name from the path list:

whereis -b -B /bin ls
whereis: option -f is missing

Add -f before the command name and rerun the lookup to confirm the result:

whereis -b -B /bin -f ls
ls: /usr/bin/ls

If a lookup returns only a name and a colon, the system is missing the binary or has stripped the documentation:

whereis -m ls
ls:

Confirm the command exists in your PATH before troubleshooting further:

command -v ls
/usr/bin/ls

Remove whereis (not recommended)

The whereis command is part of util-linux, which is essential on many distributions. Removing it can break core utilities, and package managers often block removal.

Only consider removal on disposable test systems where you can recover easily.

On Debian and Ubuntu systems, you can confirm its essential status with:

apt-cache show util-linux | grep Essential
Essential: yes

Recap whereis Usage

whereis provides a fast way to locate binaries and documentation in standard paths. Use it for quick checks, then switch to deeper tools like find when you need a full filesystem search.

Leave a Comment