⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch01_16.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 1] 1.16 Wildcards </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly &amp; Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:31:16Z"><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="ch01_01.htm"TITLE="1. Introduction"><LINKREL="prev"HREF="ch01_15.htm"TITLE="1.15 Filenames "><LINKREL="next"HREF="ch01_17.htm"TITLE="1.17 Filename Extensions "></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="ch01_15.htm"TITLE="1.15 Filenames "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 1.15 Filenames "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 1<BR>Introduction</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch01_17.htm"TITLE="1.17 Filename Extensions "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 1.17 Filename Extensions "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1009">1.16 Wildcards </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-1467"></A>The shells provide a number of <EMCLASS="emphasis">wildcards</EM> that you can use toabbreviate filenames or refer to groups of files. For example, let'ssay you want to delete all filenames in the<SPANCLASS="link">current directory (<ACLASS="linkend"HREF="ch01_21.htm"TITLE="Making Pathnames ">1.21</A>)</SPAN>ending in<EMCLASS="emphasis">.txt</EM>. You could delete these files one by one, but that wouldbe boring if there were only five and <EMCLASS="emphasis">very</EM> boring if there were ahundred. Instead, you can use a wildcarded name to say, &quot;I want allfiles whose names end with <EMCLASS="emphasis">.txt</EM>, regardless of whatthe first part is.&quot; The wildcard is the &quot;regardless&quot; part.Like a wildcard in a poker game, a wildcard in a filename can haveany value.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-1475"></A><ACLASS="indexterm"NAME="AUTOID-1478"></A>The wildcard you see most often is <CODECLASS="literal">*</CODE> (asterisk), but we'll start withsomething simpler: <CODECLASS="literal">?</CODE> (question mark).When it appears in a filename, the<CODECLASS="literal">?</CODE> matches any single character. For example,<CODECLASS="literal">letter?</CODE> refers to any filename that begins with<EMCLASS="emphasis">letter</EM> and has one character after that. This wouldinclude <EMCLASS="emphasis">letterA</EM>, <EMCLASS="emphasis">letter1</EM>, as well as filenames with anon-printing character as their last letter, like<EMCLASS="emphasis">letter</EM>^C.</P><PCLASS="para">The <CODECLASS="literal">*</CODE> wildcard matches any character or group of zero or morecharacters. For example, <CODECLASS="literal">*.txt</CODE> matches all files whosenames end with <EMCLASS="emphasis">.txt</EM>, <CODECLASS="literal">*.c</CODE> matches all fileswhose names end with <EMCLASS="emphasis">.c</EM> (by convention, source code for programsin the C language), and so on.</P><PCLASS="para">The <CODECLASS="literal">*</CODE> and <CODECLASS="literal">?</CODE> wildcards are sufficient for 90 percent of thesituations that you will find. However, there are some situations thatthey can't handle. For example, you may want to list files whosenames end with <EMCLASS="emphasis">.txt</EM>, <EMCLASS="emphasis">mail</EM>, or <EMCLASS="emphasis">let</EM>. There's no wayto do this with a single <CODECLASS="literal">*</CODE>; it won't let you exclude the files you don'twant. In this situation, use a separate <CODECLASS="literal">*</CODE> with each filename ending:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">*.txt *mail *let</PRE></BLOCKQUOTE></P><PCLASS="para">Sometimes you need to match a particular group of characters.For example,you may want to list all filenames that begin with digits, or allfilenames that begin with uppercase letters. Let's assume that you wantto work with the files <CODECLASS="literal">program.</CODE><CODECLASS="replaceable"><I>n</I></CODE>, where <CODECLASS="replaceable"><I>n</I></CODE> is asingle-digit number. Use the filename:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">program.[0123456789]</PRE></BLOCKQUOTE></P><PCLASS="para">In other words, the wildcard<CODECLASS="literal">[</CODE><CODECLASS="replaceable"><I>character-list</I></CODE><CODECLASS="literal">]</CODE>matches anysingle character that appears in the list. The character list can beany group of ASCII characters; however, if they are consecutive (e.g.,A-Z, a-z, 0-9, or 3-5, for that matter), you can use a hyphen asshorthand for the range. For example, <CODECLASS="literal">[a-zA-Z]</CODE> means any alphabeticcharacter.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-1517"></A>There is one exception to these wildcarding rules. Wildcardsnever match <CODECLASS="literal">/</CODE>, which is both the name of the<SPANCLASS="link">filesystem root (<ACLASS="linkend"HREF="ch01_19.htm"TITLE="The Tree Structure of the Filesystem ">1.19</A>)</SPAN>andthe character used to separate directory names in a<SPANCLASS="link">path (<ACLASS="linkend"HREF="ch01_21.htm"TITLE="Making Pathnames ">1.21</A>)</SPAN>.</P><PCLASS="para">If you are new to computers, you probably will catch on to UNIXwildcarding quickly. If you have used any other computer system, youhave to watch out for one very important detail. Virtually allcomputer systems except for UNIX consider a period (<CODECLASS="literal">.</CODE>) a special<ACLASS="indexterm"NAME="AUTOID-1525"></A>characterwithin a filename. Many operating systems even require a filename tohave a period in it. With these operating systems, a <CODECLASS="literal">*</CODE> does notmatch a period; you have to say <CODECLASS="literal">*.*</CODE>. Therefore, the equivalent of<CODECLASS="literal">rm&nbsp;*</CODE> does virtually nothing on most operating systems. UnderUNIX, it is very dangerous: it means &quot;delete all the files in thecurrent directory, regardless of their name.&quot; You only want to givethis command when you really mean it.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-1532"></A>But here's the exception to the exception. The shells and the <EMCLASS="emphasis">ls</EM>command consider a<CODECLASS="literal">.</CODE> special if it is the first character of a filename. This isoften used to hide initialization files and other files that youaren't normally concerned with; the <EMCLASS="emphasis">ls</EM> command doesn't showthese files unless you<SPANCLASS="link">ask (<ACLASS="linkend"HREF="ch16_11.htm"TITLE="Showing Hidden Files with ls -A and -a ">16.11</A>)</SPAN>for them.If a file's name begins with<CODECLASS="literal">.</CODE>,you always have to type the <CODECLASS="literal">.</CODE> explicitly. Forexample, <CODECLASS="literal">.*rc</CODE> matches all files whose names begin with <CODECLASS="literal">.</CODE>and end with <EMCLASS="emphasis">rc</EM>. This is a common convention for the names ofUNIX initialization files.</P><PCLASS="para">Table 1-1 has a summary of the different sorts of wildcards available.</P><TABLECLASS="table"><CAPTIONCLASS="table"><ACLASS="title"NAME="UPT-ART-1009-TAB-0">Table 1.1: Shell</A></CAPTION><THEADCLASS="thead"><TRCLASS="row"VALIGN="TOP"><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1">Wildcard</TH><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1">Matches</TH></TR></THEAD><TBODYCLASS="tbody"><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">?</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Any single character</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">*</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Any group of zero or more characters</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">[ab]</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Either a or b</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">[a-z]</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Any character between a and z, inclusive</TD></TR></TBODY></TABLE><PCLASS="para">Wildcards can be used at any point or points within a path.Remember, wildcards only match names that already exist.You can't use them to<SPANCLASS="link">create new files (<ACLASS="linkend"HREF="ch09_04.htm"TITLE="Use Wildcards to Create Files? ">9.4</A>)</SPAN>-though some shells have<ACLASS="indexterm"NAME="AUTOID-1567"></A><ACLASS="indexterm"NAME="AUTOID-1570"></A><SPANCLASS="link">curly braces (<CODECLASS="literal">{}</CODE>) (<ACLASS="linkend"HREF="ch09_05.htm"TITLE="Build Strings with {&nbsp;} ">9.5</A>, <ACLASS="linkend"HREF="ch15_03.htm"TITLE="Adding { } Operators to Korn (and Bourne) Shells ">15.3</A>)</SPAN>for doing that.Article<ACLASS="xref"HREF="ch01_18.htm"TITLE="Who Handles Wildcards? ">1.18</A>has more about how wildcards are handled.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">ML</SPAN></P></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="ch01_15.htm"TITLE="1.15 Filenames "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 1.15 Filenames "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="ch01_17.htm"TITLE="1.17 Filename Extensions "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 1.17 Filename Extensions "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">1.15 Filenames </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">1.17 Filename Extensions </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 &amp; 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 + -