The which and apropos utilities can help you find a command whose name you have forgotten or whose location you do not know. When multiple copies of a utility or program are present, which tells you which copy you will run.
which and type: Locate a Utility
When you give Mac OS X a command, the shell searches a list of directories for a program with that name and runs the first one it finds. This list of directories is called a search path. For information on how to change the search path, refer to "PATH: Where the Shell Looks for Programs" on page 285. If you do not change the search path, the shell searches only a standard set of directories and then stops searching. Other directories on the system may also contain useful utilities, however.
The which utility locates utilities by displaying the full pathname of the file for the utility. (Chapter 4 contains more information on pathnames and the structure of the OS X filesystem.) The local system may include several commands that have the same name. When you type the name of a command, the shell searches for the command in your search path and runs the first one it finds. You can find out which copy of the program the shell will run by using which. In the following example, which reports the location of the tar command:
$ which tar /usr/bin/tar
The which utility can be helpful when a command seems to be working in unexpected ways. By running which, you may discover that you are running a nonstandard version of a tool or a different one than you expected. (Refer to "Important Standard Directories and Files" on page 444 for a list of standard locations for executable files.) For example, if tar is not working properly and you find that you are running /usr/local/bin/tar instead of /usr/bin/tar, you might suspect that the local version is broken.
apropos: Searches for a Keyword
When you do not know the name of the command you need to carry out a particular task, you can use apropos with a keyword to search for it. This utility searches for the keyword in the short description line (the top line) of all man pages and displays those that contain a match. The man utility, when called with the -k (keyword) option, gives you the same output as apropos (it is the same command). The database apropos uses is not on the system as shipped, but is built automatically by the weekly periodic task (page 439). If you turn your machine off periodically (as with a laptop) or if it goes to sleep, the script may not be run. If you get no output from apropos, run the following command as root (or use sudo):
# /usr/libexec/makewhatis.local /usr/share/man:/usr/X11R6/man:/usr/local/man
The following example shows the output of apropos when you call it with the users keyword. The list is sorted alphabetically, with uppercase letters appearing before lowercase letters. The output includes the name of each command, the section of the manual that contains it, and the brief description from the top of the man page. This list includes the utility that you need (users) and identifies other, related tools that you might find useful:
$ apropos users Net::Netrc(3pm) - OO interface to users netrc file crontab(1) - maintain crontab files for individual users (V3) ftpcount(1) - show current number of users for each class ftpusers(5), ftpchroot(5) - tnftpd(8) access control file getusershell(3), setusershell(3), endusershell(3) - get valid user shells last(1) - indicate last logins of users and ttys mesg(1) - display (do not display) messages from other users mysqlaccess(1) - Create new users to mysql pdbedit(8) - manage the SAM database (Database of Samba Users) snmpusm(1) - creates and maintains SNMPv3 users on a network entity sudoers(5) - list of which users may execute what users(1) - list current users wall(1) - write a message to users
The whatis utility is similar to apropos but finds only complete word matches on the name of the utility.
$ whatis users users(1) - list current users