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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access