- Why the iPad Mini 7 is the ultraportable tablet to beat this holiday travel season - and it's $50 off
- The best iPads for college: Expert tested and reviewed
- One of the best mid-range sports watches I've tested is on sale for Black Friday
- This monster 240W charger has features I've never seen on other accessories (and get $60 off this Black Friday)
- This laptop power bank has served me well for years, and this Black Friday deal slashes the price in half
What you can find out asking which, whereis and whatis in Linux
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 <sys/socket.h> 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.
Copyright © 2021 IDG Communications, Inc.