172 lines
7.7 KiB
HTML
172 lines
7.7 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
|
|
<!Converted with LaTeX2HTML 95.1 (Fri Jan 20 1995) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
|
|
<HEAD>
|
|
<TITLE>3.7 Wildcards</TITLE>
|
|
</HEAD>
|
|
<BODY>
|
|
<meta name="description" value="3.7 Wildcards">
|
|
<meta name="keywords" value="gs">
|
|
<meta name="resource-type" value="document">
|
|
<meta name="distribution" value="global">
|
|
<P>
|
|
<BR> <HR><A NAME=tex2html3250 HREF="node119.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A> <A NAME=tex2html3248 HREF="node93.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A> <A NAME=tex2html3242 HREF="node117.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif"></A> <A NAME=tex2html3252 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif"></A> <A NAME=tex2html3253 HREF="node250.html"><IMG ALIGN=BOTTOM ALT="index" SRC="index_motif.gif"></A> <BR>
|
|
<B> Next:</B> <A NAME=tex2html3251 HREF="node119.html">3.8 UNIX Plumbing</A>
|
|
<B>Up:</B> <A NAME=tex2html3249 HREF="node93.html">3 Linux Tutorial</A>
|
|
<B> Previous:</B> <A NAME=tex2html3243 HREF="node117.html">3.6 Types of shells</A>
|
|
<BR> <HR> <P>
|
|
<H1><A NAME=SECTION00570000000000000000>3.7 Wildcards</A></H1>
|
|
<P>
|
|
<A NAME=2908> </A>
|
|
<A NAME=2909> </A>
|
|
<A NAME=2910> </A>
|
|
<A NAME=2911> </A>
|
|
A key feature of most Unix shells is the ability to reference more
|
|
than one filename using special characters. These so-called <b>wildcards</b>
|
|
allow you to refer to, say, all filenames which contain the character
|
|
``<tt>n</tt>''.
|
|
<P>
|
|
<A NAME=3024> </A>
|
|
<A NAME=3025> </A>
|
|
The wildcard ``<tt>*</tt>'' refers to any character or string of characters in
|
|
a filename. For example, when you use the character ``<tt>*</tt>'' in a filename,
|
|
the shell replaces it with all possible substitutions from filenames in
|
|
the directory which you're referencing.
|
|
<P>
|
|
Here's a quick example. Let's suppose that Larry has the files <tt>frog</tt>,
|
|
<tt>joe</tt>, and <tt>stuff</tt> in his current directory.
|
|
<P><TT> /home/larry# <em>ls</em> <BR>
|
|
frog joe stuff <BR>
|
|
/home/larry#
|
|
<P></TT>
|
|
<P>
|
|
To access all files with the letter ``o'' in the filename, we can use
|
|
the command
|
|
<P><TT> /home/larry# <em>ls *o*</em> <BR>
|
|
frog joe <BR>
|
|
/home/larry#
|
|
<P></TT>
|
|
As you can see, the use of the ``<tt>*</tt>'' wildcard was replaced with
|
|
all substitutions which matched the wildcard from filenames in the
|
|
current directory.
|
|
<P>
|
|
The use of ``<tt>*</tt>'' by itself simply matches all filenames, because
|
|
all characters match the wildcard.
|
|
<P><TT> /home/larry# <em>ls *</em> <BR>
|
|
frog joe stuff <BR>
|
|
/home/larry#
|
|
<P></TT>
|
|
<P>
|
|
Here are a few more examples.
|
|
<P><TT> /home/larry# <em>ls f*</em> <BR>
|
|
frog <BR>
|
|
/home/larry# <em>ls *ff</em> <BR>
|
|
stuff <BR>
|
|
/home/larry# <em>ls *f*</em> <BR>
|
|
frog stuff <BR>
|
|
/home/larry# <em>ls s*f</em> <BR>
|
|
stuff <BR>
|
|
/home/larry#
|
|
<P></TT>
|
|
<P>
|
|
<A NAME=2938> </A>
|
|
<A NAME=2939> </A>
|
|
The process of changing a ``<tt>*</tt>'' into filenames is called <b>wildcard
|
|
expansion</b> and is done by the shell. This is
|
|
important: the individual commands, such as <tt>ls</tt>, <em>never</em> see
|
|
the ``<tt>*</tt>'' in their list of parameters. The shell expands the
|
|
wildcard to include all of the filenames which match. So, the command
|
|
<P><TT> /home/larry# <em>ls *o*</em>
|
|
<P></TT>
|
|
is expanded by the shell to actually be
|
|
<P><TT> /home/larry# <em>ls frog joe</em>
|
|
<P></TT>
|
|
<P>
|
|
<A NAME=2951> </A>
|
|
One important note about the ``<tt>*</tt>'' wildcard. Using this wildcard
|
|
will <em>not</em> match filenames which begin with a single period (``<tt>.</tt>'').
|
|
These files are treated as ``hidden'' files---while they are not really
|
|
hidden, they don't show up on normal <tt>ls</tt> listings, and aren't
|
|
touched by the use of the ``<tt>*</tt>'' wildcard.
|
|
<P>
|
|
Here's an example. We already mentioned that each directory has two
|
|
special entries in it: ``<tt>.</tt>'' refers to the current directory,
|
|
and ``<tt>..</tt>'' refers to the parent directory. However, when you use
|
|
<tt>ls</tt>, these two entries don't show up.
|
|
<P><TT> /home/larry# <em>ls</em> <BR>
|
|
frog joe stuff <BR>
|
|
/home/larry#
|
|
<P></TT>
|
|
If you use the <tt>-a</tt> switch with <tt>ls</tt>, however, you can display
|
|
filenames which begin with ``<tt>.</tt>''. Observe:
|
|
<P><TT> /home/larry# <em>ls -a</em> <BR>
|
|
. .. .bash_profile .bashrc frog
|
|
joe stuff <BR>
|
|
/home/larry#
|
|
<P></TT>
|
|
Now we can see the two special entries, ``<tt>.</tt>'' and ``<tt>..</tt>'',
|
|
as well as two other ``hidden'' files---<tt>.bash_profile</tt> and
|
|
<tt>.bashrc</tt>. These two files are startup files used by <tt>bash</tt>
|
|
when larry logs in. More on them in Section <A HREF="node153.html#secinitscripts">3.13.3</A>.
|
|
<P>
|
|
Note that when we use the ``<tt>*</tt>'' wildcard, none of the filenames
|
|
beginning with ``<tt>.</tt>'' are displayed.
|
|
<P><TT> /home/larry# <em>ls *</em> <BR>
|
|
frog joe stuff <BR>
|
|
/home/larry#
|
|
<P></TT>
|
|
This is a safety feature: if the ``<tt>*</tt>'' wildcard matched filenames
|
|
beginning with ``<tt>.</tt>'', it would also match the directory names
|
|
``<tt>.</tt>'' and ``<tt>..</tt>''. This can be dangerous when using certain
|
|
commands.
|
|
<P>
|
|
<A NAME=3026> </A>
|
|
<A NAME=3027> </A>
|
|
Another wildcard is ``<tt>?</tt>''. The ``<tt>?</tt>'' wildcard will only
|
|
expand a single character. Thus, ``<tt>ls ?</tt>'' will display all one
|
|
character filenames, and ``<tt>ls termca?</tt>'' would display ``<tt>termcap</tt>''
|
|
but <em>not</em> ``<tt>termcap.backup</tt>''.
|
|
Here's another example:
|
|
<P><TT> /home/larry# <em>ls j?e</em> <BR>
|
|
joe <BR>
|
|
/home/larry# <em>ls f??g</em> <BR>
|
|
frog <BR>
|
|
/home/larry# <em>ls ????f</em> <BR>
|
|
stuff <BR>
|
|
/home/larry#
|
|
<P></TT>
|
|
<P>
|
|
As you can see, wildcards allow you to specify many files at one time.
|
|
In the simple command summary, in Section <A HREF="node115.html#seccommandsumm">3.4</A>, we
|
|
said that the <tt>cp</tt> and <tt>mv</tt> commands actually can copy or
|
|
move multiple files at one time. For example,
|
|
<P><TT> /home/larry# <em>cp /etc/s* /home/larry</em>
|
|
<P></TT>
|
|
will copy all filenames in <tt>/etc</tt> beginning with ``<tt>s</tt>'' to the
|
|
directory <tt>/home/larry</tt>. Therefore, the format of the <tt>cp</tt> command
|
|
is really
|
|
<P><TT> cp <IMG BORDER=0 ALIGN=BOTTOM ALT="" SRC="img172.gif"> <IMG BORDER=0 ALIGN=BOTTOM ALT="" SRC="img173.gif"> <IMG BORDER=0 ALIGN=BOTTOM ALT="" SRC="img174.gif"> ...<IMG BORDER=0 ALIGN=BOTTOM ALT="" SRC="img175.gif">
|
|
<IMG BORDER=0 ALIGN=BOTTOM ALT="" SRC="img176.gif"> <P></TT>
|
|
where <IMG BORDER=0 ALIGN=BOTTOM ALT="" SRC="img177.gif"> through <IMG BORDER=0 ALIGN=BOTTOM ALT="" SRC="img178.gif"> is a list of filenames to
|
|
copy, and <IMG BORDER=0 ALIGN=BOTTOM ALT="" SRC="img179.gif"> is the destination file or directory to
|
|
copy them to. <tt>mv</tt> has an identical syntax.
|
|
<P>
|
|
Note that if you are copying or moving more than one file, the
|
|
<IMG BORDER=0 ALIGN=BOTTOM ALT="" SRC="img180.gif"> must be a directory. You can only copy or move
|
|
a <em>single</em> file to another file.
|
|
<P>
|
|
<A NAME=3021> </A>
|
|
<A NAME=3022> </A>
|
|
<A NAME=3023> </A>
|
|
<P>
|
|
<BR> <HR><A NAME=tex2html3250 HREF="node119.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A> <A NAME=tex2html3248 HREF="node93.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A> <A NAME=tex2html3242 HREF="node117.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif"></A> <A NAME=tex2html3252 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif"></A> <A NAME=tex2html3253 HREF="node250.html"><IMG ALIGN=BOTTOM ALT="index" SRC="index_motif.gif"></A> <BR>
|
|
<B> Next:</B> <A NAME=tex2html3251 HREF="node119.html">3.8 UNIX Plumbing</A>
|
|
<B>Up:</B> <A NAME=tex2html3249 HREF="node93.html">3 Linux Tutorial</A>
|
|
<B> Previous:</B> <A NAME=tex2html3243 HREF="node117.html">3.6 Types of shells</A>
|
|
<BR> <HR> <P>
|
|
<BR> <HR>
|
|
<P><ADDRESS>
|
|
<I>Matt Welsh <BR>
|
|
mdw@sunsite.unc.edu</I>
|
|
</ADDRESS>
|
|
</BODY>
|