Americas

  • United States
sandra_henrystocker
Unix Dweeb

What you can find out asking which, whereis and whatis in Linux

How-To
Jun 07, 20215 mins
Linux

The where, whereis and whatis commands summarize commands, show where executables are, and point to relevant man pages.

CSO  >  Arrows that have missed their target.
Credit: Jumbo2010 / Getty Images

The which, whereis and whatis commands on a Linux system provide information about commands. They provide related but not identical information. In this post, we’ll check out the differences and provide a script for getting information that’s available from all three commands. We’ll also explore some sample commands for looking at secondary (i.e., not section 1) man pages.

which

The which command will show you the file-system location for a command’s executable. This is the file that is read and run whenever you type the command name.

$ which date
/usr/bin/date
$ which shutdown
/usr/sbin/shutdown

The which command will also report on your aliases and show you the commands they invoke.

$ which recent
alias recent=’ls -ltr | tail -10’
        /usr/bin/ls
        /usr/bin/tail

whereis

Like which, the whereis command will show you the file=system location for the executable associated with a command, but it will also tell you where the man (manual) pages are located.

In the example below, there are two or three man pages for each of the commands.

$ whereis date
date: /usr/bin/date /usr/share/man/man1/date.1.gz /usr/share/man/man1p/date.1p.gz
$ whereis shutdown
shutdown: /usr/sbin/shutdown /usr/share/man/man2/shutdown.2.gz /usr/share/man/man3p/shutdown.3p.gz /usr/share/man/man8/shutdown.8.gz

whatis

The whatis command provides brief descriptions of commands. Each description is associated with one of the man pages for that command as shown in the example below.

$ whatis date
date (1)             - print or set the system date and time
date (1p)            - write the date and time
$ whatis shutdown
shutdown (2)         - shut down part of a full-duplex connection
shutdown (3p)        - shut down socket send and receive operations
shutdown (8)         - Halt, power-off or reboot the machine

To view the primary man page for any command, just type “man” plus the name of the command, for example, “man date”. Add the man page section (e.g., “man 3p shutdown”) for a different man page section. Note that at the top of the man page you will see a confirmation of the command and the particular man page section:

$ man 3p shutdown

SHUTDOWN(3P)               POSIX Programmer’s Manual              SHUTDOWN(3P)

PROLOG
       This  manual  page is part of the POSIX Programmer’s Manual.  The Linux
       implementation of this interface may differ (consult the  corresponding
       Linux  manual page for details of Linux behavior), or the interface may
       not be implemented on Linux.

NAME
       shutdown—shut down socket send and receive operations

SYNOPSIS
       #include 

       int shutdown(int socket, int how);
…

(The output above has been truncated.)

Man pages are organized into as many as 9 sections:

1   Executable programs or shell commands
2   System calls (functions provided by the kernel)
3   Library calls (functions within program libraries)
4   Special files (usually found in /dev)
5   File formats and conventions, e.g. /etc/passwd
6   Games
7   Miscellaneous (including macro packages and conventions), e.g.
    man(7), groff(7)
8   System administration commands (usually only for root)
9   Kernel routines [Non standard]

Your search path matters

You can run all three of the which, whereis and whatis commands any time you want to know a little more about the commands you’re running and where they are stored on your system. If you run a command and get an unexpected response, checking out what executable you’re dealing with can be important.

$ date
No thanks. I’m not into dating.
$ which date
~/bin/date

Hmm! Something’s just a little off in that example. While you’re very unlikely to get a response like that shown, it is possible to have two executables on a system with the same name. In this case, which one you end up running will at least in part depend on your search path. Your personal bin directory should probably not be sitting in the first position in your $PATH variable nor should “.” (current position) because of the risk of running a command other than what you intended.

$ echo $PATH
~/bin:/usr/bin:/usr/local/sbin:/usr/sbin

Using all three commands

The script below will run all three commands and display the output in a useful format. In fact, it can accept a list of commands and display the command output for looking into each of them.

#!/bin/bash

clear

if [ $# == 0 ]; then     # if no arguments provided, prompt user
    echo -n “What command(s) are you asking about?> “
    read args
else
    args=$*
fi

for cmd in `echo $args`  # for each command entered
do
    echo “$cmd”
    echo -n “executable: “
    which $cmd
    echo -n “all files: “
    whereis $cmd | sed “s/$cmd: //“
    echo “function(s):”
    whatis $cmd
    echo “====================================================================”
done

Here’s an example of running the script for two commands:

$ about date shutdown
date
executable: ~/bin/date
all files: /usr/bin/date /home/shs/bin/date /usr/share/man/man1/date.1.gz /usr/share/man/man1p/date.1p.gz
function(s):
date (1)             - print or set the system date and time
date (1p)            - write the date and time
====================================================================
shutdown
executable: /usr/sbin/shutdown
all files: /usr/sbin/shutdown /usr/share/man/man2/shutdown.2.gz /usr/share/man/man3p/shutdown.3p.gz /usr/share/man/man8/shutdown.8.gz
function(s):
shutdown (2)         - shut down part of a full-duplex connection
shutdown (3p)        - shut down socket send and receive operations
shutdown (8)         - Halt, power-off or reboot the machine
====================================================================

Wrap-Up

The where, whereis and whatis commands provide useful summaries of commands, showing where the executables are located and pointing to the relevant man pages. The which command can also verify which executable you are using when you type a command name.

Consult the man pages to learn about all the options available and get a more complete explanation of how these commands work.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.