Finding and Running Commands

Problem

You need to find and run a particular command under bash.

Solution

Try the type, which, apropos, locate, slocate, find, and ls commands.

Discussion

bash keeps a list of directories in which it should look for commands in an environment variable called $PATH. The bash built-in type command searches your environment (including aliases, keywords, functions, built-ins, and files in the $PATH) for executable commands matching its arguments and displays the type and location of any matches. It has several arguments, notably the -a flag, which causes it to print all matches instead of stopping at the first one. The which command is similar but only searches your $PATH (and csh aliases). It may vary from system to system (it’s usually a csh shell script on BSD, but a binary on Linux), and usually has a -a flag like type. Use these commands when you know the name of a command and need to know exactly where it’s located, or to see if it’s on this computer. For example:

$ type which
which is hashed (/usr/bin/which)

$ type ls
ls is aliased to `ls -F -h'

$ type -a ls
ls is aliased to `ls -F -h'
ls is /bin/ls

$ which which
/usr/bin/which

Almost all commands come with some form of help on how to use them. Usually there is online documentation called manpages, where “man” is short for manual. These are accessed using the man command, so man ls will give you documentation about the ls command. Many programs also have a built-in help facility, accessed by providing a “help me” argument such as -h or --help. Some programs, especially on other operating systems, will give you help if you don’t give them arguments. Some Unix commands will also do that, but a great many of them will not. This is due to the way that Unix commands fit together into something called pipelines, which we’ll cover later. But what if you don’t know or can’t remember the name of the command you need? apropos searches manpage names and descriptions for regular expressions supplied as arguments. This is incredibly useful when you don’t remember the name of the command you need. This is the same as man -k.

$ apropos music
cms (4) - Creative Music System device driver

$ man -k music
cms (4) - Creative Music System device driver

locate and slocate consult database files about the system (usually compiled and updated by a cron job) to find files or commands almost instantly. The location of the actual database files, what is indexed therein, and how often it is checked, may vary from system to system. Consult your system’s manpages for details. slocate stores permission information (in addition to filenames and paths) so that it will not list programs to which the user does not have access. On most Linux systems, locate is a symbolic link to slocate; other systems may have separate programs, or may not have slocate at all.

$ locate apropos
/usr/bin/apropos
/usr/share/man/de/man1/apropos.1.gz
/usr/share/man/es/man1/apropos.1.gz
/usr/share/man/it/man1/apropos.1.gz
/usr/share/man/ja/man1/apropos.1.gz
/usr/share/man/man1/apropos.1.gz

For details on the find command, see all of Chapter 9.

Last but not least, try using ls also. Remember if the command you wish to run is in your current directory, you must prefix it with a ./ since the current working directory is usually not in your $PATH for security reasons (see Setting a Secure $PATH and Adding the Current Directory to the $PATH).

See Also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.125.2