📄 ch17_06.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 17] 17.6 Be an Expert on find Search Operators </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:38: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="ch17_01.htm"TITLE="17. Finding Files with find"><LINKREL="prev"HREF="ch17_05.htm"TITLE="17.5 Searching for Old Files "><LINKREL="next"HREF="ch17_07.htm"TITLE="17.7 The Times that find Finds "></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="ch17_05.htm"TITLE="17.5 Searching for Old Files "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 17.5 Searching for Old Files "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 17<BR>Finding Files with find</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch17_07.htm"TITLE="17.7 The Times that find Finds "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 17.7 The Times that find Finds "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1580">17.6 Be an Expert on find Search Operators </A></H2><PCLASS="para"><EMCLASS="emphasis">find</EM> is admittedly tricky. Once you get a handle on itsabilities, you'll learn to appreciate its trickiness. But before thinking about anything remotely tricky, let's look at asimple <EMCLASS="emphasis">find</EM> command:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find . -name "*.c" -print</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">The <CODECLASS="literal">.</CODE> tells <EMCLASS="emphasis">find</EM> to start its search in<SPANCLASS="link">the current directory (<CODECLASS="literal">.</CODE>) (<ACLASS="linkend"HREF="ch01_21.htm"TITLE="Making Pathnames ">1.21</A>)</SPAN>,and to search all subdirectories of the currentdirectory.The<SPANCLASS="link"><CODECLASS="literal">-name "*.c"</CODE> (<ACLASS="linkend"HREF="ch17_04.htm"TITLE="Looking for Files with Particular Names ">17.4</A>)</SPAN>tells <EMCLASS="emphasis">find</EM> to findfiles whose names end in <EMCLASS="emphasis">.c</EM>.The <EMCLASS="emphasis">-print</EM> operator tells <EMCLASS="emphasis">find</EM> how to handlewhat it finds: print the names on standard output.</P><PCLASS="para">All <EMCLASS="emphasis">find</EM> commands, no matter how complicated, are really justvariations on the one above.You can specify many different names, look forold files, and so on; no matter how complex, you're really onlyspecifying a starting point, some search parameters, and what to dowith the files (or directories or links or...) you find.</P><PCLASS="para">The key to using <EMCLASS="emphasis">find</EM> in a more sophisticated way is realizingthat search parameters are really "logical expressions" that <EMCLASS="emphasis">find</EM>evaluates.That is, <EMCLASS="emphasis">find</EM>:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">Looks at every file, one at a time.</P></LI><LICLASS="listitem"><PCLASS="para">Uses the information in the file's<SPANCLASS="link">inode (<ACLASS="linkend"HREF="ch01_22.htm"TITLE="How UNIX Keeps Track of Files: Inodes ">1.22</A>)</SPAN>to evaluate an expression given by the command-line operators.</P></LI><LICLASS="listitem"><PCLASS="para">Takes the specified action (e.g., printing the file's name) if theexpression's value is "true."</P></LI></UL><PCLASS="para">So, something like <CODECLASS="literal">-name <">*.c<"></CODE> is really a logical expressionthat evaluates to true if the file's name ends in <EMCLASS="emphasis">.c</EM>.</P><PCLASS="para">Once you've gotten used to thinking this way, it's easy to use theAND, OR, NOT, and grouping operators. So let's think about amore complicated <EMCLASS="emphasis">find</EM> command. Let's look for files that end in <EMCLASS="emphasis">.o</EM> or <EMCLASS="emphasis">.tmp</EM> AND that are more than five days old,AND print their pathnames. We want an expression that evaluates true for fileswhose names match either <EMCLASS="emphasis">*.o</EM> OR <EMCLASS="emphasis">*.tmp</EM>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">-name "*.o" -o -name "*.tmp"</PRE></BLOCKQUOTE></P><PCLASS="para">If either condition is true, we want to check the access time.So we put the expression above within parentheses(<SPANCLASS="link">quoted (<ACLASS="linkend"HREF="ch08_14.htm"TITLE="Bourne Shell Quoting ">8.14</A>)</SPAN>with backslashes so the shell doesn't treat the parentheses as<SPANCLASS="link">subshell operators (<ACLASS="linkend"HREF="ch13_07.htm"TITLE="The () Subshell Operators ">13.7</A>)</SPAN>).We also add a<SPANCLASS="link"><EMCLASS="emphasis">-atime</EM> operator (<ACLASS="linkend"HREF="ch17_05.htm"TITLE="Searching for Old Files ">17.5</A>)</SPAN>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">-atime +5 \( -name "*.o" -o -name "*.tmp" \)</PRE></BLOCKQUOTE></P><PCLASS="para">The parentheses force <EMCLASS="emphasis">find</EM> to evaluate what's inside as a unit.The expression is true if "the access time is more than 5 daysago and \( either the name ends with <EMCLASS="emphasis">.o</EM> or the name ends with<EMCLASS="emphasis">.tmp</EM> \)."If you didn't use parentheses, the expression would mean something different:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">-atime +5 -name "*.o" -o -name "*.tmp" <EMCLASS="emphasis">Wrong!</EM></PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-18666"></A>When <EMCLASS="emphasis">find</EM> sees two operators next to each other with no <EMCLASS="emphasis">-o</EM> between,that means AND.So the "wrong" expression is true if "either \( the access time is morethan 5 days ago and the name ends with <EMCLASS="emphasis">.o</EM> \) or the name ends with<EMCLASS="emphasis">.tmp</EM>."This incorrect expression would be true for any name ending with<EMCLASS="emphasis">.tmp</EM>, no matter how recently the file was accessed - the<CODECLASS="literal">-atime</CODE> doesn't apply.(There's nothing really "wrong" or illegal in this secondexpression - except that it's not what we want.<EMCLASS="emphasis">find</EM> will accept the expression and do what we asked - it just won'tdo what we want.)</P><PCLASS="para">The following command, which is what we want, lists files in the currentdirectory and subdirectories that match our criteria:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find . -atime +5 \( -name "*.o" -o -name "*.tmp" \) -print</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">What if we wanted to list all files that do <EMCLASS="emphasis">not</EM> match thesecriteria? All we want is the logical inverse of this expression. The<ACLASS="indexterm"NAME="AUTOID-18682"></A><ACLASS="indexterm"NAME="AUTOID-18685"></A>NOT operator is <CODECLASS="literal">!</CODE> (exclamation point). The <CODECLASS="literal">!</CODE> operatorapplies to the expression on its right. Since we want it to apply to the entire expression, and not just the <EMCLASS="emphasis">-atime</EM> operator,we'll have to group everything from <CODECLASS="literal">-atime</CODE> to <CODECLASS="literal">"*.tmp"</CODE>within another set of parentheses.</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find . ! \( -atime +5 \( -name "*.o" -o -name "*.tmp" \) \) -print</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">For that matter, even <EMCLASS="emphasis">-print</EM> is an expression; it always evaluatesto true.So are<SPANCLASS="link"><EMCLASS="emphasis">-exec</EM> and <EMCLASS="emphasis">-ok</EM> (<ACLASS="linkend"HREF="ch17_10.htm"TITLE="Running Commands on What You Find ">17.10</A>)</SPAN>;they evaluate to truewhen the command they execute returns a zero status. (There are afew situations in which this can be used to good effect; see article<ACLASS="xref"HREF="ch17_11.htm"TITLE="Using -exec to Create Custom Tests ">17.11</A>for some of those.)Article<ACLASS="xref"HREF="ch17_12.htm"TITLE="Finding Many Things with One Command ">17.12</A>has more about <EMCLASS="emphasis">find</EM> expressions.</P><PCLASS="para">But before you try anything too complicated, you need to realize onething.<EMCLASS="emphasis">find</EM> isn't as sophisticated as you might like it to be.You can't squeeze all the spaces out of expressions, as if it were a<ACLASS="indexterm"NAME="AUTOID-18706"></A>real programming language. You need spaces before and after operatorslike <CODECLASS="literal">!</CODE>, <CODECLASS="literal">\(</CODE>, <CODECLASS="literal">\)</CODE>, and <CODECLASS="literal">{}</CODE>, in addition tospaces before and after every other operator.Therefore, a command linelike the following won't work:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find . !\(-atime +5 \(-name "*.o" -o -name "*.tmp"\)\) -print</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">A true power user will realize that <EMCLASS="emphasis">find</EM> is relying on theshell to<SPANCLASS="link">separate the command line into meaningful chunks (<ACLASS="linkend"HREF="ch08_05.htm"TITLE="Command-Line Evaluation ">8.5</A>)</SPAN>,<ACLASS="indexterm"NAME="AUTOID-18719"></A><ACLASS="indexterm"NAME="AUTOID-18721"></A>or <EMCLASS="emphasis">tokens</EM>.And the shell, inturn, is assuming that tokens are separated by spaces. Whenthe shell gives <EMCLASS="emphasis">find</EM> a chunk of characters like<CODECLASS="literal">*.tmp))</CODE> (without the double quotes or backslashes - the shell took themaway), <EMCLASS="emphasis">find</EM> getsconfused; it thinks you're talking about a weird filename pattern thatincludes a couple of parentheses.</P><PCLASS="para">Once you start thinking about expressions, <EMCLASS="emphasis">find</EM>'s syntax ceasesto be obscure - in some ways, it's even elegant. It certainly allowsyou to say what you need to say with reasonable efficiency.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">ML</SPAN>, <SPANCLASS="authorinitials">JP</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="ch17_05.htm"TITLE="17.5 Searching for Old Files "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 17.5 Searching for Old Files "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="ch17_07.htm"TITLE="17.7 The Times that find Finds "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 17.7 The Times that find Finds "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">17.5 Searching for Old Files </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">17.7 The Times that find Finds </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 + -