📄 ch17_23.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 17] 17.23 Finding Files with -prune </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:43Z"><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_22.htm"TITLE="17.22 Finding the Links to a File "><LINKREL="next"HREF="ch17_24.htm"TITLE="17.24 Skipping Some Parts of a Tree in find (A More Selective -prune)"></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_22.htm"TITLE="17.22 Finding the Links to a File "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 17.22 Finding the Links to a File "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_24.htm"TITLE="17.24 Skipping Some Parts of a Tree in find (A More Selective -prune)"><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 17.24 Skipping Some Parts of a Tree in find (A More Selective -prune)"BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-0473">17.23 Finding Files with -prune </A></H2><PCLASS="para"><EMCLASS="emphasis">find</EM> has lots of operators for finding some particular kinds of files.<ACLASS="indexterm"NAME="AUTOID-19834"></A>But <EMCLASS="emphasis">find</EM> won't stop at your current directory - if there aresubdirectories, it looks there too.How can you tell it "only the current directory"?Use <EMCLASS="emphasis">-prune</EM>.</P><BLOCKQUOTECLASS="note"><PCLASS="para"><STRONG>NOTE:</STRONG> If your version of <EMCLASS="emphasis">find</EM> doesn't have <EMCLASS="emphasis">-prune</EM>, this won't work.Try GNU <EMCLASS="emphasis">find</EM> on the CD-ROM.</P></BLOCKQUOTE><PCLASS="para"><EMCLASS="emphasis">-prune</EM> prunes <EMCLASS="emphasis">find</EM>'s search tree at the current pathname.So, if the current pathname is a directory, <EMCLASS="emphasis">find</EM> won't descend into thatdirectory for any further searches.The command line looks kind of hairy.Here's one to find all files from the current directory modified in the last24 hours:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find . \( -type d ! -name . -prune \) -o \( -mtime -1 -print \)</B></CODE>./afile./cfile</PRE></BLOCKQUOTE></P><PCLASS="para">I'll put that into an alias in a minute.First let's try to understand it - once you see the pattern, you'll understandsome<SPANCLASS="link">important things (<ACLASS="linkend"HREF="ch08_05.htm"TITLE="Command-Line Evaluation ">8.5</A>, <ACLASS="linkend"HREF="ch17_12.htm"TITLE="Finding Many Things with One Command ">17.12</A>)</SPAN>about <EMCLASS="emphasis">find</EM> that many people don't.Let's follow <EMCLASS="emphasis">find</EM> as it looks at a few pathnames.</P><PCLASS="para"><EMCLASS="emphasis">find</EM> looks at each entry, one by one, in the current directory(<CODECLASS="literal">.</CODE>).For each entry, <EMCLASS="emphasis">find</EM> tries to match the expression from left to right.As soon as some part matches, it ignores the rest (if any)of the expression.</P><OLCLASS="orderedlist"><LICLASS="listitem"><PCLASS="para">When <EMCLASS="emphasis">find</EM> is looking at the file named <EMCLASS="emphasis">./afile</EM>:The first part of the expression, <CODECLASS="literal">\( -type d ! -name . -prune \)</CODE>,doesn't match (<EMCLASS="emphasis">./afile</EM> isn't a directory).So <EMCLASS="emphasis">find</EM> tries the other part, after the <CODECLASS="literal">-o</CODE> (or).</P><PCLASS="para">Has <EMCLASS="emphasis">./afile</EM> been modified in the last day?In this case, it has - so the <CODECLASS="literal">-print</CODE> (which is always true) printsthe pathname.</P></LI><LICLASS="listitem"><PCLASS="para">Next, <EMCLASS="emphasis">./bfile</EM>:Like the previous step, the first part of the expression won't match.In the second part, <CODECLASS="literal">\( -mtime -1 -print \)</CODE>, the file's modification time is more than one day ago.So this part of the expression is false; <EMCLASS="emphasis">find</EM> doesn't botherwith the <CODECLASS="literal">-print</CODE> operator.</P></LI><LICLASS="listitem"><PCLASS="para">Finally, let's look at <EMCLASS="emphasis">./adir</EM>, a directory:The first part of the expression, <CODECLASS="literal">\( -type d ! -name . -prune \)</CODE>,matches.That's because <EMCLASS="emphasis">./adir</EM> is a directory (<CODECLASS="literal">-type d</CODE>),its name is not <CODECLASS="literal">.</CODE> (<CODECLASS="literal">! -name .</CODE>)-and so <CODECLASS="literal">-prune</CODE>,which is always true, makes this part of the expression true.<EMCLASS="emphasis">find</EM> skips <EMCLASS="emphasis">./adir</EM>.</P></LI></OL><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-19890"></A>Your <EMCLASS="emphasis">find</EM> may also have a <EMCLASS="emphasis">-maxdepth</EM> option that gives themaximum number of directory levels to descend.For example, <CODECLASS="literal">find . -maxdepth 0</CODE> operates only on the currentdirectory.</P><PCLASS="para">Let's put the version with <EMCLASS="emphasis">-prune</EM> into a couple of aliases.The first one, named <EMCLASS="emphasis">find.</EM> (with a dot on the end of its name), justprints names with <EMCLASS="emphasis">-print</EM>.The second alias gives a listing like <EMCLASS="emphasis">ls -gilds</EM>.They work like this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find. -mtime -1</B></CODE>./afile./cfile% <CODECLASS="userinput"><B>find.ls -mtime -1</B></CODE>43073 0 -r---- 1 jerry ora 0 Mar 27 18:16 ./afile43139 2 -r-r-r- 1 jerry ora 1025 Mar 24 02:33 ./cfile</PRE></BLOCKQUOTE></P><PCLASS="para">The <EMCLASS="emphasis">find.</EM> alias is handy inside<SPANCLASS="link">backquotes (<ACLASS="linkend"HREF="ch09_16.htm"TITLE="Command Substitution ">9.16</A>)</SPAN>,feeding a pipe, and other places you need a list of filenames.Here are the aliases.The second one, <EMCLASS="emphasis">find.ls</EM>, uses <EMCLASS="emphasis">-ls</EM> instead of <EMCLASS="emphasis">-print</EM>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"> alias find. 'find . \( -type d ! -name . -prune \) -o \( \!* -print \)'alias find.ls 'find . \( -type d ! -name . -prune \) -o \( \!* -ls \)'</PRE></BLOCKQUOTE></P><PCLASS="para">(The Bourne shell versions on the CD-ROMare named <EMCLASS="emphasis">Find</EM> and <EMCLASS="emphasis">Findls</EM> because a dot isn't legal in afunction name.)</P><PCLASS="para">If you don't want the <CODECLASS="literal">./</CODE> at the start of each name, add a pipethrough<SPANCLASS="link"><CODECLASS="literal">colrm 1 2</CODE> (<ACLASS="linkend"HREF="ch35_15.htm"TITLE="Cutting Columns with colrm ">35.15</A>)</SPAN>or<SPANCLASS="link"><CODECLASS="literal">cut -c3-</CODE> (<ACLASS="linkend"HREF="ch35_14.htm"TITLE="Cutting Columns or Fields with cut ">35.14</A>)</SPAN>to the end of the alias definition.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <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_22.htm"TITLE="17.22 Finding the Links to a File "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 17.22 Finding the Links to a File "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_24.htm"TITLE="17.24 Skipping Some Parts of a Tree in find (A More Selective -prune)"><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 17.24 Skipping Some Parts of a Tree in find (A More Selective -prune)"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">17.22 Finding the Links to a File </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.24 Skipping Some Parts of a Tree in find (A More Selective -prune)</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 + -