📄 ch15_01.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 15] Wildcards</TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:37:19Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="part03.htm"TITLE="III. Working with the Filesystem "><LINKREL="prev"HREF="ch14_14.htm"TITLE="14.14 Automatic Setup When You Enter/Exit a Directory "><LINKREL="next"HREF="ch15_02.htm"TITLE="15.2 Filename Wildcards in a Nutshell "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch14_14.htm"TITLE="14.14 Automatic Setup When You Enter/Exit a Directory "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 14.14 Automatic Setup When You Enter/Exit a Directory "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 15</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch15_02.htm"TITLE="15.2 Filename Wildcards in a Nutshell "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 15.2 Filename Wildcards in a Nutshell "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="CHAPTER"><H1CLASS="chapter"><ACLASS="title"NAME="UPT-CHP-15">15. Wildcards</A></H1><DIVCLASS="htmltoc"><P><B>Contents:</B><BR><ACLASS="sect1"HREF="#UPT-ART-1480"TITLE="15.1 File Naming Wildcards ">File Naming Wildcards </A><BR><ACLASS="sect1"HREF="ch15_02.htm"TITLE="15.2 Filename Wildcards in a Nutshell ">Filename Wildcards in a Nutshell </A><BR><ACLASS="sect1"HREF="ch15_03.htm"TITLE="15.3 Adding { } Operators to Korn (and Bourne) Shells ">Adding { } Operators to Korn (and Bourne) Shells </A><BR><ACLASS="sect1"HREF="ch15_04.htm"TITLE="15.4 What if a Wildcard Doesn't Match? ">What if a Wildcard Doesn't Match? </A><BR><ACLASS="sect1"HREF="ch15_05.htm"TITLE='15.5 Matching All "Dot Files" with Wildcards'>Matching All "Dot Files" with Wildcards</A><BR><ACLASS="sect1"HREF="ch15_06.htm"TITLE="15.6 Maybe You Shouldn't Use Wildcards in Pathnames ">Maybe You Shouldn't Use Wildcards in Pathnames </A><BR><ACLASS="sect1"HREF="ch15_07.htm"TITLE="15.7 Getting a List of Matching Files with grep -l ">Getting a List of Matching Files with grep -l </A><BR><ACLASS="sect1"HREF="ch15_08.htm"TITLE="15.8 Getting a List of Non-Matching Files with grep -c ">Getting a List of Non-Matching Files with grep -c </A><BR><ACLASS="sect1"HREF="ch15_09.htm"TITLE="15.9 nom: List Files that Don't Match a Wildcard ">nom: List Files that Don't Match a Wildcard </A><BR><ACLASS="sect1"HREF="ch15_10.htm"TITLE="15.10 Wildcards that Match Only Directories ">Wildcards that Match Only Directories </A></P><P></P></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1480">15.1 File Naming Wildcards </A></H2><PCLASS="para"><SPANCLASS="link">Wildcards (<ACLASS="linkend"HREF="ch01_16.htm"TITLE="Wildcards ">1.16</A>)</SPAN><ACLASS="indexterm"NAME="UPT-ART-1480-IX-WILDCARDS-FILE-NAMING"></A><ACLASS="indexterm"NAME="UPT-ART-1480-IX-FILENAMES-WILDCARDS-AND"></A><ACLASS="indexterm"NAME="AUTOID-15986"></A>are the shell's way of abbreviating filenames.Just as in poker,where a wildcard is a special card that can match any card in the deck, filename wildcards are capable ofmatching letters, or groups of letters, in the alphabet. Rather thantyping a long filename, or a long chain of filenames, a wildcard letsyou provide parts of names, and then use some "wildcard characters" forthe rest. For example, if you want to delete all files whose namesend in <EMCLASS="emphasis">.o</EM>, you can give the command:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>rm *.o</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">You don't have to list every filename.</P><PCLASS="para">I'm sure you already know that wildcards are useful in manysituations. If not, they are summarized in article<ACLASS="xref"HREF="ch15_02.htm"TITLE="Filename Wildcards in a Nutshell ">15.2</A>.Here are a few of my favorite wildcard applications:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">If you remember part of a filename, but not the whole name, you canuse wildcards to help you find it. If I have a file on genetics savedin a directory with several hundred other files, a command like: </P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>ls *gene*</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">will often find what I want. It's quicker and easier than<SPANCLASS="link"><EMCLASS="emphasis">find</EM> (<ACLASS="linkend"HREF="ch17_01.htm#UPT-ART-7410"TITLE="The find Command Is Great; The Problem Is Finding How to Use It">17.1</A>)</SPAN>.</P></LI><LICLASS="listitem"><PCLASS="para">Wildcards are a natural when you want to work with groups of files.If I have a general purpose directory that's full of filenames endingin <EMCLASS="emphasis">.c</EM> and <EMCLASS="emphasis">.h</EM>, I can make new subdirectories and usewildcards to move the files easily:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>mkdir c h</B></CODE>% <CODECLASS="userinput"><B>mv *.c c</B></CODE>% <CODECLASS="userinput"><B>mv *.h h</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para"></P></LI><LICLASS="listitem"><PCLASS="para">Wildcards often help you to work with files with inconvenient characters intheir names. Let's say you have a file named <CODECLASS="literal">abc</CODE><CODECLASS="replaceable"><I>x</I></CODE><CODECLASS="literal">e</CODE>, where<CODECLASS="replaceable"><I>x</I></CODE> is some unknown control character. You can delete orrename that file by using the wildcarded name <CODECLASS="literal">abc?e</CODE>. (When youdo this, be careful that your wildcard doesn't match more than you intend.)</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-16025"></A><ACLASS="indexterm"NAME="AUTOID-16028"></A>Wildcards can appear in any component of a pathname. This can oftenbe used to your advantage. For example, let's say that you have adirectory named <EMCLASS="emphasis">/work</EM>, split into subdirectories for a dozendifferent projects. For each project, you have a schedule, in a filecalled (obviously enough) <EMCLASS="emphasis">schedule.txt</EM>. You can print all theschedules with the command:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>lpr /work/*/schedule.txt </B></CODE><EMCLASS="emphasis">BSD UNIX</EM>% <CODECLASS="userinput"><B>lp /work/*/schedule.txt </B></CODE><EMCLASS="emphasis">System V UNIX</EM></PRE></BLOCKQUOTE></P><PCLASS="para">(However, you can occasionally<SPANCLASS="link">run into problems (<ACLASS="linkend"HREF="ch15_06.htm"TITLE="Maybe You Shouldn't Use Wildcards in Pathnames ">15.6</A>)</SPAN>.)</P></LI></UL><PCLASS="para">It's a common misconception, particularly among new users, thatapplication programs and utilities have something to do withwildcards.Given a command like <CODECLASS="literal">grep ident *.c</CODE>, many users thinkthat <EMCLASS="emphasis">grep</EM> handles the <CODECLASS="literal">*</CODE> and looks to see which files have namesthat end in <EMCLASS="emphasis">.c</EM>. If you're at all familiar with UNIX'sworkings, you'll realize that this is the wrong picture. The shellinterprets wildcards.That is, the shell figures out which files havenames ending in <EMCLASS="emphasis">.c</EM>, puts them in a list, puts that list on thecommand line, and then hands that command line to <EMCLASS="emphasis">grep</EM>. As itprocesses the command line, the shell turns <CODECLASS="literal">grep ident *.c</CODE> into <CODECLASS="literal">grep ident file1.c file2.c ...</CODE>.</P><PCLASS="para">Since there are several shells, one might think (or fear!) that thereshould be several different sets of wildcards. Fortunately, therearen't. The C shell has made onesignificant extension (the<SPANCLASS="link">curly brace operators (<ACLASS="linkend"HREF="ch09_05.htm"TITLE="Build Strings with { } ">9.5</A>)</SPAN>),and the Korn shell has made a few more, but thebasic wildcards work the same for all shells.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">ML</SPAN></P></DIV></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch14_14.htm"TITLE="14.14 Automatic Setup When You Enter/Exit a Directory "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 14.14 Automatic Setup When You Enter/Exit a Directory "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch15_02.htm"TITLE="15.2 Filename Wildcards in a Nutshell "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 15.2 Filename Wildcards in a Nutshell "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">14.14 Automatic Setup When You Enter/Exit a Directory </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">15.2 Filename Wildcards in a Nutshell </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed & awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -