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

📄 ch17_18.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 17] 17.18 Using "Fast find&quot;</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:33Z"><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_17.htm"TITLE="17.17 Duplicating a Directory Tree (No Pathnames with find {} Operator)"><LINKREL="next"HREF="ch17_19.htm"TITLE="17.19 Finding Files (Much) Faster with a find Database "></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_17.htm"TITLE="17.17 Duplicating a Directory Tree (No Pathnames with find {} Operator)"><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 17.17 Duplicating a Directory Tree (No Pathnames with find {} Operator)"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_19.htm"TITLE="17.19 Finding Files (Much) Faster with a find Database "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 17.19 Finding Files (Much) Faster with a find Database "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1620">17.18 Using &quot;Fast find&quot;</A></H2><PCLASS="para"><ACLASS="indexterm"NAME="UPT-ART-1620-IX-FAST-FIND"></A><ACLASS="indexterm"NAME="UPT-ART-1620-IX-FIND-COMMAND-FAST-FIND"></A>Berkeley added a handy feature to its <EMCLASS="emphasis">find</EM> command&nbsp;- if you give it a single argument, it will search a database for file ordirectory names that match.(If your system doesn't have this feature, see the <EMCLASS="emphasis">locate</EM> utility below.)For example, if you know there's a file named <EMCLASS="emphasis">MH.eps</EM> somewhere onthe computer but you don't know where, type:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find MH.eps</B></CODE>/nutshell/graphics/cover/MH.eps</PRE></BLOCKQUOTE></P><PCLASS="para">The database is usually rebuilt every night.So, it's not completely up-to-date, but it's usually close enough.If your system administrator has set this up, the database usually listsall files on the filesystem&nbsp;- although it may not list files indirectories that don't have world-access permission.If the database isn't set up at all, you'll get an error like<CODECLASS="literal">/usr/lib/find/find.codes: No such file or directory</CODE>.(If that's the case, you can set up a &quot;fast <EMCLASS="emphasis">find</EM>&quot; database yourself.Use GNU <EMCLASS="emphasis">locate</EM>, below, or seearticle<ACLASS="xref"HREF="ch17_19.htm"TITLE="Finding Files (Much) Faster with a find Database ">17.19</A>.)</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-19403"></A><ACLASS="indexterm"NAME="AUTOID-19406"></A>Unless you use wildcards, fast <EMCLASS="emphasis">find</EM> does a simple string search, like<SPANCLASS="link"><EMCLASS="emphasis">fgrep</EM> (<ACLASS="linkend"HREF="ch27_06.htm"TITLE="Fast grep Isn't ">27.6</A>)</SPAN>,through a list of<SPANCLASS="link">absolute pathnames (<ACLASS="linkend"HREF="ch14_02.htm"TITLE="Using Relative and Absolute Pathnames ">14.2</A>)</SPAN>.Here's an extreme example:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find bin</B></CODE>/bin/bin/ar   .../home/robin/home/robin/afile/home/sally/bin   ...</PRE></BLOCKQUOTE></P><PCLASS="para">You can cut down this output by piping it through<SPANCLASS="link"><EMCLASS="emphasis">grep</EM> (<ACLASS="linkend"HREF="ch27_01.htm#UPT-ART-7420"TITLE="Different Versions of grep ">27.1</A>)</SPAN>,<SPANCLASS="link"><EMCLASS="emphasis">sed</EM> (<ACLASS="linkend"HREF="ch34_24.htm"TITLE="Quick Reference: sed ">34.24</A>)</SPAN>,and so on.All the fast <EMCLASS="emphasis">find</EM> commands I've used have an undocumented feature, though:they can match<SPANCLASS="link">shell wildcards (<CODECLASS="literal">*</CODE>, <CODECLASS="literal">?</CODE>, <CODECLASS="literal">[]</CODE>) (<ACLASS="linkend"HREF="ch15_02.htm"TITLE="Filename Wildcards in a Nutshell ">15.2</A>)</SPAN>.If you use a wildcard on one end of the pattern, the search pattern isautomatically &quot;anchored&quot; to the opposite end of the string (the end where thewildcard isn't).The shell matches filenames in the same way.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-19427"></A>The difference between the shell's wildcard matching and fast <EMCLASS="emphasis">find</EM>'smatching is that the shell treats slashes (<CODECLASS="literal">/</CODE>) specially: youhave to type them as part of the expression.In fast <EMCLASS="emphasis">find</EM>, a wildcard matches slashes and any other character.When you use a wildcard, be sure to put quotes around the pattern so the shellwon't touch it.</P><PCLASS="para">&#13;Here are some examples:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">To find any pathname that ends with <EMCLASS="emphasis">bin</EM>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find '*bin'</B></CODE>/bin/home/robin/home/robin/bin   ...</PRE></BLOCKQUOTE></P><PCLASS="para"></P></LI><LICLASS="listitem"><PCLASS="para">To find any pathname that ends with <EMCLASS="emphasis">/bin</EM> (a good way to find a file ordirectory named exactly <EMCLASS="emphasis">bin</EM>):</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find '*/bin'</B></CODE>/bin/home/robin/bin/usr/bin   ...</PRE></BLOCKQUOTE></P><PCLASS="para"></P></LI><LICLASS="listitem"><PCLASS="para">Typing <CODECLASS="literal">find&nbsp;'*bin*'</CODE> is the same as typing <CODECLASS="literal">find&nbsp;bin</CODE>.</P></LI><LICLASS="listitem"><PCLASS="para">To match the files in a directory named <EMCLASS="emphasis">bin</EM>, but not the directoryitself, try something like:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find '*/bin/*'</B></CODE>/bin/ar/bin/cat   .../home/robin/bin/prog</PRE></BLOCKQUOTE></P><PCLASS="para"></P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-19463"></A><ACLASS="indexterm"NAME="AUTOID-19466"></A>To find the files in <EMCLASS="emphasis">/home</EM> whose names end with a tilde (<CODECLASS="literal">~</CODE>)(these are probably backup files from the Emacs editor):</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find '/home/*~'</B></CODE>/home/testfile~/home/allan/.cshrc~/home/allan/.login~/home/dave/.profile~   ...</PRE></BLOCKQUOTE></P><PCLASS="para">Notice that the fast <EMCLASS="emphasis">find</EM> asterisk matches &quot;dot files,&quot; too.</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-19478"></A><ACLASS="indexterm"NAME="AUTOID-19481"></A>The <CODECLASS="literal">?</CODE> (question mark) and <CODECLASS="literal">[]</CODE> (square brackets) operatorswork, too.They're not quite as useful as they are in the shell because they matchthe slashes (<CODECLASS="literal">/</CODE>) in the pathnames.Here are a couple of quick examples:&#13;</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find '????'</B></CODE>/bin/etc/lib/src/sys/usr% <CODECLASS="userinput"><B>find '/[bel]??'</B></CODE>/bin/etc/lib</PRE></BLOCKQUOTE></P></LI></UL><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="locate">locate</A><BR></TH><TDVALIGN="TOP"><ACLASS="indexterm"NAME="AUTOID-19493"></A>Unfortunately, not all systems have fast find.Fortunately, the Free Software Foundation has <EMCLASS="emphasis">locate</EM>.It's similar to fast find, but<EMCLASS="emphasis">locate</EM> has an advantage: you can have multiple file databasesand you can search some or all of them.<EMCLASS="emphasis">Locate</EM> comes with a database building program.</TD></TR></TABLE><PCLASS="para">Because fast <EMCLASS="emphasis">find</EM> and <EMCLASS="emphasis">locate</EM> areso fast, they're worth trying to use whenever you can.Pipe the output to<SPANCLASS="link"><EMCLASS="emphasis">xargs</EM> (<ACLASS="linkend"HREF="ch09_21.htm"TITLE="Handle Too-Long Command Lines with xargs ">9.21</A>)</SPAN>and any other UNIX command,run a shell or <EMCLASS="emphasis">awk</EM> script to test its output&nbsp;- almost anything willbe faster than running a standard <EMCLASS="emphasis">find</EM>.For example, if you want a long listing of the files, here are two<EMCLASS="emphasis">find</EM> commands to do it:</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> <ACLASS="co"HREF="ch09_16.htm"TITLE="9.16 Command Substitution ">`...`</A> &#13;</PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">% <CODECLASS="userinput"><B>ls -l `find <CODECLASS="replaceable"><I>whatever</I></CODE>`</B></CODE>% <CODECLASS="userinput"><B>find </B></CODE><CODECLASS="replaceable"><I>whatever </I></CODE><CODECLASS="userinput"><B>| xargs ls -ld</B></CODE></PRE></TD></TR></TABLE></P><PCLASS="para">There's one problem with that trick.The fast <EMCLASS="emphasis">find</EM> list may be built by <EMCLASS="emphasis">root</EM>,which can see all thefiles on the filesystem; your <EMCLASS="emphasis">ls -l</EM> commandmay not be able to access all files in the list.<ACLASS="indexterm"NAME="AUTOID-19519"></A><ACLASS="indexterm"NAME="AUTOID-19520"></A></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_17.htm"TITLE="17.17 Duplicating a Directory Tree (No Pathnames with find {} Operator)"><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 17.17 Duplicating a Directory Tree (No Pathnames with find {} Operator)"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_19.htm"TITLE="17.19 Finding Files (Much) Faster with a find Database "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 17.19 Finding Files (Much) Faster with a find Database "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">17.17 Duplicating a Directory Tree (No Pathnames with find {} Operator)</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.19 Finding Files (Much) Faster with a find Database </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 + -