% Linux Installation and Getting Started    -*- TeX -*-
% wildcard.tex
% Copyright (c) 1992, 1993 by Matt Welsh, Larry Greenfield and Karl Fogel
%
% This file is freely redistributable, but you must preserve this copyright 
% notice on all copies, and it must be distributed only as part of "Linux 
% Installation and Getting Started". This file's use is covered by
% the copyright for the entire document, in the file "copyright.tex".

\section{Wildcards}

\index{shells!wildcard characters for|(}
\index{wildcard characters!in filnames|(}
\index{filenames!wildcard characters in|(}
\index{wildcard characters!defined}
A key feature of most Unix shells is the ability to reference more
than one filename using special characters. These so-called {\bf wildcards}
allow you to refer to, say, all filenames which contain the character
``{\tt n}''. 

\index{*@{\tt *}}
\index{wildcard characters!*@{\tt *}}
The wildcard ``{\tt *}'' refers to any character or string of characters in 
a filename. For example, when you use the character ``{\tt *}'' in a filename, 
the shell replaces it with all possible substitutions from filenames in
the directory which you're referencing.

Here's a quick example. Let's suppose that Larry has the files {\tt frog},
{\tt joe}, and {\tt stuff} in his current directory.
\begin{tscreen}
/home/larry\# {\em ls} \\
frog\ \ \ \ \ joe\ \ \ \ \ stuff \\
/home/larry\#
\end{tscreen}

To access all files with the letter ``o'' in the filename, we can use
the command
\begin{tscreen}
/home/larry\# {\em ls *o*} \\
frog\ \ \ \ \ joe \\
/home/larry\#
\end{tscreen}
As you can see, the use of the ``{\tt *}'' wildcard was replaced with
all substitutions which matched the wildcard from filenames in the
current directory. 

The use of ``{\tt *}'' by itself simply matches all filenames, because
all characters match the wildcard.
\begin{tscreen}
/home/larry\# {\em ls *} \\
frog\ \ \ \ \ joe\ \ \ \ \ stuff \\
/home/larry\#
\end{tscreen}

Here are a few more examples.
\begin{tscreen}
/home/larry\# {\em ls f*} \\
frog \\
/home/larry\# {\em ls *ff} \\
stuff \\
/home/larry\# {\em ls *f*} \\
frog\ \ \ \ \ stuff \\
/home/larry\# {\em ls s*f} \\
stuff \\
/home/larry\# 
\end{tscreen}

\index{wildcard expansion!defined}
\index{shells!wildcard expansion}
The process of changing a ``{\tt *}'' into filenames is called {\bf wildcard 
expansion} and is done by the shell. This is
important: the individual commands, such as {\tt ls}, {\em never\/} see 
the ``{\tt *}'' in their list of parameters. The shell expands the
wildcard to include all of the filenames which match. So, the command
\begin{tscreen}
/home/larry\# {\em ls *o*}
\end{tscreen}
is expanded by the shell to actually be
\begin{tscreen}
/home/larry\# {\em ls frog joe}
\end{tscreen}

\index{files!hidden!not matched by wildcards}
One important note about the ``{\tt *}'' wildcard. Using this wildcard
will {\em not} match filenames which begin with a single period (``{\tt .}''). 
These files are treated as ``hidden'' files---while they are not really
hidden, they don't show up on normal {\tt ls} listings, and aren't
touched by the use of the ``{\tt *}'' wildcard. 

Here's an example. We already mentioned that each directory has two
special entries in it: ``{\tt .}'' refers to the current directory,
and ``{\tt ..}'' refers to the parent directory. However, when you use
{\tt ls}, these two entries don't show up.
\begin{tscreen}
/home/larry\# {\em ls} \\
frog\ \ \ \ \ joe\ \ \ \ \ stuff \\
/home/larry\# 
\end{tscreen}
If you use the {\tt -a} switch with {\tt ls}, however, you can display
filenames which begin with ``{\tt .}''. Observe:
\begin{tscreen}
/home/larry\# {\em ls -a} \\
.\ \ \ \ \ ..\ \ \ \ \ .bash\_profile\ \ \ \ \ .bashrc\ \ \ \ \ frog
\ \ \ \ \ joe\ \ \ \ \ stuff \\
/home/larry\# 
\end{tscreen}
Now we can see the two special entries, ``{\tt .}'' and ``{\tt ..}'',
as well as two other ``hidden'' files---{\tt .bash\_profile} and
{\tt .bashrc}. These two files are startup files used by {\tt bash}
when larry logs in. More on them in Section~\ref{sec-init-scripts}.

Note that when we use the ``{\tt *}'' wildcard, none of the filenames
beginning with ``{\tt .}'' are displayed.
\begin{tscreen}
/home/larry\# {\em ls *} \\
frog\ \ \ \ \ joe\ \ \ \ \ stuff \\
/home/larry\# 
\end{tscreen}
This is a safety feature: if the ``{\tt *}'' wildcard matched filenames
beginning with ``{\tt .}'', it would also match the directory names
``{\tt .}'' and ``{\tt ..}''. This can be dangerous when using certain
commands.

\index{wildcard characters!?@{\tt ?}}
\index{?@{\tt ?}}
Another wildcard is ``{\tt ?}''.  The ``{\tt ?}'' wildcard will only
expand a single character. Thus, ``{\tt ls ?}'' will display all one 
character filenames, and ``{\tt ls termca?}'' would display ``{\tt termcap}''
but {\em not\/} ``{\tt termcap.backup}''.
Here's another example:
\begin{tscreen}
/home/larry\# {\em ls j?e} \\
joe \\
/home/larry\# {\em ls f??g} \\
frog \\
/home/larry\# {\em ls ????f} \\
stuff \\
/home/larry\# 
\end{tscreen}

As you can see, wildcards allow you to specify many files at one time.
In the simple command summary, in Section~\ref{sec-command-summ}, we
said that the {\tt cp} and {\tt mv} commands actually can copy or
move multiple files at one time. For example,
\begin{tscreen}
/home/larry\# {\em cp /etc/s* /home/larry}
\end{tscreen}
will copy all filenames in {\tt /etc} beginning with ``{\tt s}'' to the
directory {\tt /home/larry}. Therefore, the format of the {\tt cp} command 
is really
\begin{tscreen}
cp \cparam{file1} \cparam{file2} \cparam{file3} \ldots \cparam{fileN} 
  \cparam{destination}
\end{tscreen}
where \cparam{file1} through \cparam{fileN} is a list of filenames to
copy, and \cparam{destination} is the destination file or directory to 
copy them to. {\tt mv} has an identical syntax.

Note that if you are copying or moving more than one file, the
\cparam{destination} must be a directory. You can only copy or move
a {\em single\/} file to another file.


\index{shells!wildcard characters for|)}
\index{wildcard characters!in filnames|)}
\index{filenames!wildcard characters in|)}
