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

📄 ch17_12.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 17] 17.12 Finding Many Things with One Command </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:38:27Z"><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_11.htm"TITLE="17.11 Using -exec to Create Custom Tests "><LINKREL="next"HREF="ch17_13.htm"TITLE="17.13 Searching for Files by Type "></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_11.htm"TITLE="17.11 Using -exec to Create Custom Tests "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 17.11 Using -exec to Create Custom Tests "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_13.htm"TITLE="17.13 Searching for Files by Type "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 17.13 Searching for Files by Type "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-0290">17.12 Finding Many Things with One Command </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-19020"></A>Running <EMCLASS="emphasis">find</EM> is fairly time-consuming, and for good reason: it has toread every<SPANCLASS="link">inode (<ACLASS="linkend"HREF="ch01_22.htm"TITLE="How UNIX Keeps Track of Files: Inodes ">1.22</A>)</SPAN>in the directory tree that it's searching.Therefore,it's a good idea to combine as many things as you can into a single<EMCLASS="emphasis">find</EM> command. If you're going to walk the entire tree, you mayas well accomplish as much as possible in the process.</P><PCLASS="para">Let's work from an example. Assume that you want to write a command(eventually for inclusion in a shell script) that sets file <SPANCLASS="link">access modes (<ACLASS="linkend"HREF="ch22_02.htm"TITLE="Tutorial on File and Directory Permissions ">22.2</A>)</SPAN>correctly.You want to change all directories to 771 access, 600 access for allbackup files (<EMCLASS="emphasis">*.BAK</EM>), 755 access for all shell scripts(<EMCLASS="emphasis">*.sh</EM>), and 644 access for all text files (<EMCLASS="emphasis">*.txt</EM>).Youcan do all this with one command:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>find . \( -type d       -a -exec chmod 771 {} \; \) -o \         \( -name &quot;*.BAK&quot; -a -exec chmod 600 {} \; \) -o \         \( -name &quot;*.sh&quot;  -a -exec chmod 755 {} \; \) -o \         \( -name &quot;*.txt&quot; -a -exec chmod 644 {} \; \)</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">Why does this work? Remember that <EMCLASS="emphasis">-exec</EM> is really justanother part of the expression; it evaluates to true when thefollowing command is successful. It isn't an independent actionthat somehow applies to the whole <EMCLASS="emphasis">find</EM> operation. Therefore,<EMCLASS="emphasis">exec</EM> canbe mixed freely with <EMCLASS="emphasis">-type</EM>, <EMCLASS="emphasis">-name</EM>, and so on.(Also, see article<ACLASS="xref"HREF="ch17_11.htm"TITLE="Using -exec to Create Custom Tests ">17.11</A>.)</P><PCLASS="para">However, there's another important trick here. Look at the firstchunk of the command. It says: &quot;If this file is a directory and the<EMCLASS="emphasis">chmod</EM> command executes successfully...&quot; Wait. Why doesn't the<EMCLASS="emphasis">-exec</EM> execute a <EMCLASS="emphasis">chmod</EM> on every file in the directory,trying to see whether or not it's successful?</P><PCLASS="para">Logical expressions are evaluated from left to right; and, in anychunk of the expression, evaluation stops once it's clear what theoutcome is.Consider the logical expression &quot;`A AND B' is true.&quot;If A is false, you know that the result of &quot;`A AND B' is true&quot; will also befalse&nbsp;- so there'sno need to look at B.</P><PCLASS="para">So in the multi-layered expression above,when <EMCLASS="emphasis">find</EM> is looking at a file, it checks whether or notthe file is a directory.If it is, <CODECLASS="literal">-type&nbsp;d</CODE> is true, and <EMCLASS="emphasis">find</EM> evaluatesthe <EMCLASS="emphasis">-exec</EM> (changing the file's mode). If the file is not adirectory, <EMCLASS="emphasis">find</EM> knows that the result of the entire statementwill be false, so it doesn't bother wasting time with the<EMCLASS="emphasis">-exec</EM>.<EMCLASS="emphasis">find</EM> goes on to the next chunk.</P><PCLASS="para">And, of course, there's no need for the <EMCLASS="emphasis">-exec</EM>s to run the samekind of command. Some could delete files, some could change modes,some could move them to another directory, and so on.</P><PCLASS="para">One final point. Although understanding our multi-layered <EMCLASS="emphasis">find</EM> expression wasdifficult, it really was no different from a &quot;garden variety&quot; command.Think about what the following command means:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find . -name &quot;*.c&quot; -print</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-19062"></A><ACLASS="indexterm"NAME="AUTOID-19065"></A>There are two operators: <EMCLASS="emphasis">-name</EM>(which evaluates to true if the file's name ends in <EMCLASS="emphasis">.c</EM>) and<EMCLASS="emphasis">-print</EM> (which is always true). The two operators are ANDedtogether; we could stick a <EMCLASS="emphasis">-a</EM> between the two without changingthe result at all.If <EMCLASS="emphasis">-name</EM> evaluates tofalse (i.e., if the file's name doesn't end in <EMCLASS="emphasis">.c</EM>),<EMCLASS="emphasis">find</EM> knows that the entire expression will be false. Soit doesn't bother with <EMCLASS="emphasis">-print</EM>. But if <EMCLASS="emphasis">-name</EM> evaluates totrue, <EMCLASS="emphasis">find</EM> evaluates <EMCLASS="emphasis">-print</EM>-which, as a side effect,prints the name.</P><PCLASS="para">As we said in article<ACLASS="xref"HREF="ch17_06.htm"TITLE="Be an Expert on find Search Operators ">17.6</A>,<EMCLASS="emphasis">find</EM>'s business is evaluatingexpressions&nbsp;- not locating files. Yes, <EMCLASS="emphasis">find</EM> certainly locatesfiles; but that's really just a side effect.For me, understanding this point was the conceptualbreakthrough that made <EMCLASS="emphasis">find</EM> much more useful.</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="ch17_11.htm"TITLE="17.11 Using -exec to Create Custom Tests "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 17.11 Using -exec to Create Custom Tests "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_13.htm"TITLE="17.13 Searching for Files by Type "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 17.13 Searching for Files by Type "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">17.11 Using -exec to Create Custom Tests </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.13 Searching for Files by Type </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 + -