Locating Commands

 < Day Day Up > 

The whereis and apropos utilities help you find a command whose name you have forgotten or whose location you do not know. When there are multiple copies of a utility or program, which can tell you which copy you will run. The slocate 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 284. 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 (commands) by displaying the full pathname to the file for the utility. (Chapter 4 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. (Refer to "Important Standard Directories and Files" on page 86 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 /bin/tar, you might suspect that the local version is broken.

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.

caution: which, whereis, and builtin commands

Both the which and whereis utilities report only the names for commands as they are found on disk and do not report shell builtins (utilities that are built into a shell; see page 132). When you use whereis to try to find out 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 487) to determine whether a command is a builtin.

 $ type echo echo is a shell builtin 


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 more than one program with the specified name is in the search path, 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 a keyword and the apropos utility to search for it. (The whatis database has to be set up and regularly maintained with makewhatis for apropos to work; this task is typically handled by cron. Refer to crontab on page 624 for more information.) This utility searches for the keyword in the short description line (the top line) of all of the man pages and displays those that contain a match. The man utility, when called with the k (keyword) option, displays the same output as apropos (it is actually the same command).

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 

slocate: Searches for a File

The slocate utility, a secure version of locate, searches for files on the local system:

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

Before you can use slocate the updatedb utility must build/update the slocate database. Typically the database is updated once a day by a cron script. Refer to crontab on page 624 for more information.

     < Day Day Up > 


    A Practical Guide to LinuxR Commands, Editors, and Shell Programming
    A Practical Guide to LinuxR Commands, Editors, and Shell Programming
    ISBN: 131478230
    EAN: N/A
    Year: 2005
    Pages: 213

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