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

📄 ch17_10.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 17] 17.10 Running Commands on What You Find </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:23Z"><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_09.htm"TITLE="17.9 Problems with -newer "><LINKREL="next"HREF="ch17_11.htm"TITLE="17.11 Using -exec to Create Custom Tests "></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_09.htm"TITLE="17.9 Problems with -newer "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 17.9 Problems with -newer "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_11.htm"TITLE="17.11 Using -exec to Create Custom Tests "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 17.11 Using -exec to Create Custom Tests "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1590">17.10 Running Commands on What You Find </A></H2><PCLASS="para"><EMCLASS="emphasis">[Often, when you find a file, you don't just want to see its name; youwant to do something, like </EM>grep<EMCLASS="emphasis"> for a text string. To do this,use the </EM>-exec<EMCLASS="emphasis"> operator. Thisallows you to specify a command that is executedupon each file that is found. -TOR&nbsp;]</EM></P><PCLASS="para"><ACLASS="indexterm"NAME="UPT-ART-1590-IX-FIND-COMMAND-EXEC-OPTION-EXEC"></A>The syntax is peculiar and in many cases, it is simpler just to<SPANCLASS="link">pipe the output of <EMCLASS="emphasis">find</EM> to <EMCLASS="emphasis">xargs</EM> (<ACLASS="linkend"HREF="ch17_02.htm"TITLE="Delving Through a Deep Directory Tree ">17.2</A>)</SPAN>.However, there are cases where <EMCLASS="emphasis">-exec</EM> is just the thing, so let's plungein and explain its peculiarities.</P><PCLASS="para">The<EMCLASS="emphasis">-exec</EM>operator allows you to execute any command, includinganother <EMCLASS="emphasis">find</EM> command.If you consider that for a moment, you realize that<EMCLASS="emphasis">find</EM>needs some way to distinguish the command it's executing from itsown arguments.The obvious choice is to use the same end-of-command character as theshell (i.e., the semicolon).Since the shell<SPANCLASS="link">uses the semicolon (<ACLASS="linkend"HREF="ch08_05.htm"TITLE="Command-Line Evaluation ">8.5</A>)</SPAN>itself, it is necessary toescape the character with a backslash or quotes.</P><PCLASS="para">Therefore, every <EMCLASS="emphasis">-exec</EM> operator ends with the characters <CODECLASS="literal">\;</CODE>.There is one more special argument that<EMCLASS="emphasis">find</EM>treats differently:<ACLASS="indexterm"NAME="AUTOID-18870"></A><CODECLASS="literal">{}</CODE>.These two characters are used as the variable whose name is the file<EMCLASS="emphasis">find</EM>found.Don't bother rereading that last line.An example will clarify the usage.The following is a trivial case, and uses the<EMCLASS="emphasis">-exec</EM>operator with<SPANCLASS="link"><EMCLASS="emphasis">echo</EM> (<ACLASS="linkend"HREF="ch08_06.htm"TITLE="Output Command-Line Arguments ">8.6</A>)</SPAN>to mimic the<EMCLASS="emphasis">-print</EM>operator:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find . -exec echo {} \;</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">The C shelluses the characters<SPANCLASS="link"><CODECLASS="literal">{</CODE> and <CODECLASS="literal">}</CODE> (<ACLASS="linkend"HREF="ch09_05.htm"TITLE="Build Strings with {&nbsp;} ">9.5</A>)</SPAN>,but doesn't change<CODECLASS="literal">{}</CODE> together,which is why it is not necessary to quote these characters.The semicolon must be quoted, however.Quotes can be used instead of a backslash:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find . -exec echo {} ';'</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">as both will sneak the semicolon past the shell and get it to the<EMCLASS="emphasis">find</EM>command.As I said before,<EMCLASS="emphasis">find</EM>can even call<EMCLASS="emphasis">find</EM>.If you wanted to list every symbolic link in every directory owned by a group<EMCLASS="emphasis">staff</EM>,you could execute:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch09_16.htm"TITLE="9.16 Command Substitution ">`...`</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">% <CODECLASS="userinput"><B>find `pwd` -type d -group staff -exec find {} -type l -print \;</B></CODE></PRE></TD></TR></TABLE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-18900"></A><ACLASS="indexterm"NAME="AUTOID-18903"></A><ACLASS="indexterm"NAME="AUTOID-18906"></A>To search for all files with group-write permission and remove thepermission,you can use:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch17_15.htm"TITLE="17.15 Searching for Files by Permission ">-perm</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">% <CODECLASS="userinput"><B>find . -perm -20 -exec chmod g-w {} \;</B></CODE></PRE></TD></TR></TABLE></P><PCLASS="para">or:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find . -perm -20 -print | xargs chmod g-w </B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">The difference between<EMCLASS="emphasis">-exec</EM>and<EMCLASS="emphasis">xargs</EM>is subtle.The first one will execute the program once per file, while<EMCLASS="emphasis">xargs</EM>can handle several files with each process.However,<EMCLASS="emphasis">xargs</EM>may have<SPANCLASS="link">problems (<ACLASS="linkend"HREF="ch09_22.htm"TITLE="xargs: Problems with Spaces and Newlines ">9.22</A>)</SPAN>with filenames that contain embedded spaces.</P><PCLASS="para">Occasionally people create a strange file that they can't delete.This could be caused by accidentally creating a file with a spaceor some control character in the name.<EMCLASS="emphasis">find</EM>and<EMCLASS="emphasis">-exec</EM>can delete this file, while<EMCLASS="emphasis">xargs</EM>could not.In this case, use<EMCLASS="emphasis">ls -il</EM>to list the files and<SPANCLASS="link">i-numbers (<ACLASS="linkend"HREF="ch01_22.htm"TITLE="How UNIX Keeps Track of Files: Inodes ">1.22</A>)</SPAN>,and use the<ACLASS="indexterm"NAME="AUTOID-18930"></A><SPANCLASS="link"><EMCLASS="emphasis">-inum</EM> operator with <EMCLASS="emphasis">-exec</EM> (<ACLASS="linkend"HREF="ch23_16.htm"TITLE="Removing a Strange File by its I-number ">23.16</A>)</SPAN>to delete the file:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find . -inum 31246 -exec rm {} ';'</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-18940"></A>If you wish, you can use<EMCLASS="emphasis">-ok</EM>which does the same as<EMCLASS="emphasis">-exec</EM>,except the program asks you first to confirm the actionbefore executing the command.It is a good idea to be cautious when using<EMCLASS="emphasis">find</EM>,because the program can make a mistake into a disaster.When in doubt, use<EMCLASS="emphasis">echo</EM>as the command.Or send the output to a file and examine the filebefore using the file as input to<EMCLASS="emphasis">xargs</EM>.This is how I discovered that<EMCLASS="emphasis">find</EM>requires<CODECLASS="literal">{}</CODE>to stand alonein the arguments to<EMCLASS="emphasis">-exec</EM>.I wanted to rename some files using<CODECLASS="literal">-exec&nbsp;mv&nbsp;{}&nbsp;{}.orig</CODE>but <EMCLASS="emphasis">find</EM> wouldn't replace the <CODECLASS="literal">{}</CODE> in <CODECLASS="literal">{}.orig</CODE>.I learned that I have to<SPANCLASS="link">write a shell script (<ACLASS="linkend"HREF="ch17_11.htm"TITLE="Using -exec to Create Custom Tests ">17.11</A>)</SPAN>that I tell<EMCLASS="emphasis">find</EM>to execute.[A little Bourne shell<SPANCLASS="link"><EMCLASS="emphasis">while</EM> loop (<ACLASS="linkend"HREF="ch44_10.htm"TITLE="Loops That Test Exit Status ">44.10</A>)</SPAN>with<SPANCLASS="link">redirected input (<ACLASS="linkend"HREF="ch45_23.htm"TITLE="The Ins and Outs of Redirected I/O Loops ">45.23</A>)</SPAN>can handle that too:&#13;</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist">&#13;<ACLASS="co"HREF="ch17_15.htm"TITLE="17.15 Searching for Files by Permission ">&gt;</A> &#13;</PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">$ <CODECLASS="userinput"><B>find ... -print |</B></CODE>&gt; <CODECLASS="userinput"><B>while read file</B></CODE>&gt; <CODECLASS="userinput"><B>do mv &quot;$file&quot; &quot;$file.orig&quot;</B></CODE>&gt; <CODECLASS="userinput"><B>done</B></CODE></PRE></TD></TR></TABLE></P><PCLASS="para"><EMCLASS="emphasis">find</EM> writes the filenames to its standard output.The <EMCLASS="emphasis">while</EM> loop and its<SPANCLASS="link"><EMCLASS="emphasis">read</EM> command (<ACLASS="linkend"HREF="ch44_13.htm"TITLE="read: Reading from the Keyboard ">44.13</A>)</SPAN>read the filenames from standard input, then make themavailable as <CODECLASS="literal">$file</CODE>, one by one. <EMCLASS="emphasis">-JP</EM>&nbsp;]</P><PCLASS="para">Articles<ACLASS="xref"HREF="ch17_12.htm"TITLE="Finding Many Things with One Command ">17.12</A>and<ACLASS="xref"HREF="ch17_24.htm"TITLE="Skipping Some Parts of a Tree in find (A More Selective -prune)">17.24</A>have more examples of <EMCLASS="emphasis">-exec</EM>.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">BB</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_09.htm"TITLE="17.9 Problems with -newer "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 17.9 Problems with -newer "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_11.htm"TITLE="17.11 Using -exec to Create Custom Tests "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 17.11 Using -exec to Create Custom Tests "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">17.9 Problems with -newer </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.11 Using -exec to Create Custom Tests </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 + -