📄 ch17_02.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 17] 17.2 Delving Through a Deep Directory Tree </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: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="ch17_01.htm"TITLE="17. Finding Files with find"><LINKREL="prev"HREF="ch17_01.htm"TITLE="17.1 The find Command Is Great; The Problem Is Finding How to Use It"><LINKREL="next"HREF="ch17_03.htm"TITLE="17.3 Don't Forget -print "></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_01.htm"TITLE="17.1 The find Command Is Great; The Problem Is Finding How to Use It"><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 17.1 The find Command Is Great; The Problem Is Finding How to Use It"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_03.htm"TITLE="17.3 Don't Forget -print "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 17.3 Don't Forget -print "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-0412">17.2 Delving Through a Deep Directory Tree </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-18375"></A><ACLASS="indexterm"NAME="AUTOID-18379"></A><ACLASS="indexterm"NAME="AUTOID-18382"></A>The first, and most obvious, use is <EMCLASS="emphasis">find</EM>'sability to locate old, big, or unused files whose locations you'veforgotten. However, what may be <EMCLASS="emphasis">find</EM>'s most fundamentally importantcharacteristic is itsability to travel down subdirectories.</P><PCLASS="para">Normally the shell provides the argument list to a command.That is, UNIX programs are frequently given filenames and not directory names.Only a few programs can be given a directory name and march down the directorysearching for subdirectories. The programs<EMCLASS="emphasis">find</EM>,<EMCLASS="emphasis">tar</EM>,<EMCLASS="emphasis">du</EM>,and <EMCLASS="emphasis">diff</EM>do this.Some versions of<EMCLASS="emphasis">chmod</EM>,<EMCLASS="emphasis">chgrp</EM>,<EMCLASS="emphasis">ls</EM>,<EMCLASS="emphasis">rm</EM>,and<EMCLASS="emphasis">cp</EM>will, but only if a<EMCLASS="emphasis">-r</EM>or<EMCLASS="emphasis">-R</EM>option is specified.</P><PCLASS="para">In general, most commands do not understand directory structures, and rely on the shell toexpand<ACLASS="indexterm"NAME="AUTOID-18400"></A><SPANCLASS="link">wildcards (<ACLASS="linkend"HREF="ch15_02.htm"TITLE="Filename Wildcards in a Nutshell ">15.2</A>)</SPAN>to directory names.That is, to delete all files whose names end with a <EMCLASS="emphasis">.o</EM> in a group of directories,you could type:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>rm *.o */*.o */*/*.o</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">Not only is this tedious to type, it may not find all of the filesyou are searching for. The shell has certain blind spots.It will not match files in directories whose names start with a dot.And, if any files match<CODECLASS="literal">*/*/*/*.o</CODE>,they would not be deleted.</P><PCLASS="para">Another problemis typing the above and getting the error <CODECLASS="literal">Arguments</CODE> <CODECLASS="literal">too</CODE> <CODECLASS="literal">long.</CODE>This means the shell would expand too many arguments from the wildcardsyou typed.</P><PCLASS="para"><EMCLASS="emphasis">find</EM> is the answer to these problems.</P><PCLASS="para">A simple example of <EMCLASS="emphasis">find</EM> is using it to print the names of all the filesin the directory and all subdirectories. This is done with the simple command:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find . -print</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">The first arguments to <EMCLASS="emphasis">find</EM>are directory and file pathnames - in that example,<SPANCLASS="link">a dot (<CODECLASS="literal">.</CODE>) is one name for the current directory (<ACLASS="linkend"HREF="ch01_21.htm"TITLE="Making Pathnames ">1.21</A>)</SPAN>.The arguments after the pathnames always start with a minus sign (<CODECLASS="literal">-</CODE>) and tell <EMCLASS="emphasis">find</EM> what to do once it finds a file.These are the search operators.In this case, the filename is printed.You can use the<SPANCLASS="link">tilde (<CODECLASS="literal">~</CODE>) (<ACLASS="linkend"HREF="ch14_11.htm"TITLE="Finding (Anyone's) Home Directory, Quickly ">14.11</A>)</SPAN>supported by the C shell, as well asparticular paths. For example:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find ~ ~barnett /usr/local -print</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">And if you have a very slow day, you can type:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find / -print</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">which will list every file on the system.[This command is okay on single-user workstations with their own disks.It can tie up disks on multiuser systems enough to make users think ofgruesome crimes!If you really need that list and your system has<SPANCLASS="link">fast <EMCLASS="emphasis">find</EM> (<ACLASS="linkend"HREF="ch17_18.htm"TITLE='Using "Fast find"'>17.18</A>)</SPAN>,try the command <CODECLASS="literal">find '/*'</CODE> instead. <EMCLASS="emphasis">-JP</EM> ]</P><PCLASS="para"><EMCLASS="emphasis">find </EM>sends its output to standard output, so once you've "found" a list of filenames, you can pass them to other commands.One way to use this is with <SPANCLASS="link"> command substitution : (<ACLASS="linkend"HREF="ch09_16.htm"TITLE="Command Substitution ">9.16</A>)</SPAN></P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch16_08.htm"TITLE="16.8 The ls -d Option ">-d</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">% <CODECLASS="userinput"><B>ls -l `find . -print`</B></CODE></PRE></TD></TR></TABLE></P><PCLASS="para">The<EMCLASS="emphasis">find</EM>command is executed, and its output replaces the backquoted string.<EMCLASS="emphasis">ls</EM>sees the output of <EMCLASS="emphasis">find</EM>,and doesn't even know <EMCLASS="emphasis">find</EM>was used.</P><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="xref.warning"HREF="ch09_22.htm"TITLE="9.22 xargs: Problems with Spaces and Newlines ">Warning!</A> </TH><TDVALIGN="TOP">An alternate method uses the<ACLASS="indexterm"NAME="AUTOID-18454"></A><ACLASS="indexterm"NAME="AUTOID-18457"></A><SPANCLASS="link"><EMCLASS="emphasis">xargs</EM> (<ACLASS="linkend"HREF="ch09_21.htm"TITLE="Handle Too-Long Command Lines with xargs ">9.21</A>)</SPAN>command.<EMCLASS="emphasis">xargs</EM>and <EMCLASS="emphasis">find</EM>work together beautifully.<EMCLASS="emphasis">xargs</EM>executes its arguments as commands and reads standard input to specify arguments to that command.<EMCLASS="emphasis">xargs</EM>knows the maximum number of arguments each command line can handleand does not exceed that limit.While the command:</TD></TR></TABLE><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>ls -ld `find / -print`</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">might generate an error when the command line is too large, the equivalent command using <EMCLASS="emphasis">xargs</EM>will never generate that error:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find / -print | xargs ls -ld</B></CODE></PRE></BLOCKQUOTE></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_01.htm"TITLE="17.1 The find Command Is Great; The Problem Is Finding How to Use It"><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 17.1 The find Command Is Great; The Problem Is Finding How to Use It"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_03.htm"TITLE="17.3 Don't Forget -print "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 17.3 Don't Forget -print "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">17.1 The find Command Is Great; The Problem Is Finding How to Use It</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.3 Don't Forget -print </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 + -