Locating Commands


The whereis 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. The locate utility searches for files on the local system.

which and whereis: Locate a Utility

When you give Linux 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 292. 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.

which


The which utility locates utilities by displaying the full pathname of the file for the utility. (Chapter 6 contains more information on pathnames and the structure of the Linux 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 /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. ("Important Standard Directories and Files" on page 176 provides 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 /bin/tar, you might suspect that the local version is broken.

Caution: which, whereis, and builtin commands

Both the which and whereis utilities report only the names for commands as they are found on the disk; they do not report shell builtins (utilities that are built into a shell; see page 225). When you use whereis to try to find where the echo command (which exists as both a utility program and a shell builtin) is kept, you get the following result:

$ whereis echo echo: /bin/echo /usr/share/man/man1/echo.1.gz /usr/share/man/man1p/echo.1p. gz /usr/share/man/man3/echo.3x.gz


The whereis utility does not display the echo builtin. Even the which utility reports the wrong information:

$ which echo /bin/echo


Under bash you can use the type builtin (page 927) to determine whether a command is a builtin:

$ type echo echo is a shell builtin



whereis


The whereis utility searches for files related to a utility by looking in standard locations instead of using your search path. For example, you can find the locations for files related to tar:

$ whereis tar tar: /bin/tar/usr/include/tar.h /usr/share/man/man1/tar.1.gz


In this example whereis finds three references to tar: the tar utility file, a tar header file, and the tar man page.

Tip: which versus whereis

Given the name of a program, which looks through the directories in your search path, in order, and locates the program. If the search path includes more than one program with the specified name, which displays the name of only the first one (the one you would run).

The whereis utility looks through a list of standard directories and works independently of your search path. Use whereis to locate a binary (executable) file, any manual pages, and source code for a program you specify; whereis displays all the files it finds.


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, named whatis, is not on Red Hat Linux systems when they are first installed, but is built automatically by cron (page 547) using makewhatis. (The cron utility runs the /etc/cron.weekly/makewhatis.cron script to build the whatis database.) If you turn the system off periodically (as with a laptop), the script may not be run. If apropos does not produce any output, run the command makewhatis w as root.

The following example shows the output of apropos when you call it with the who keyword. 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 (who) and identifies other, related tools that you might find useful:

$ apropos who at.allow [at]         (5)  - determine who can submit jobs via at or batch at.deny [at]          (5)  - determine who can submit jobs via at or batch jwhois                (1)  - client for the whois service ldapwhoami            (1)  - LDAP who am i? tool w                     (1)  - Show who is logged on and what they are doing who                   (1)  - show who is logged on whoami                (1)  - print effective userid


whatis


The whatis utility is similar to apropos but finds only complete word matches for the name of the utility:

$ whatis who who                  (1)  - show who is logged on


locate: Searches for a File

The locate utility searches for files on the local system:

$ locate motd /etc/motd /lib/security/pam_motd.so /usr/share/man/man5/motd.5.gz


Before you can use locate the updatedb utility must build or update the locate database. Typically the database is updated once a day by a cron script (page 547).

Tip: If you are not on a network, skip the rest of this chapter

If you are the only user on a system that is not connected to a network, you may want to skip the rest of this chapter. If you are not on a network but are set up to send and receive email, read "Email" page 152.





A Practical Guide to Red Hat Linux
A Practical Guide to Red HatВ® LinuxВ®: Fedoraв„ў Core and Red Hat Enterprise Linux (3rd Edition)
ISBN: 0132280272
EAN: 2147483647
Year: 2006
Pages: 383

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net