📄 ch17_21.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 17] 17.21 lookfor: Which File Has that Word? </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:40Z"><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_20.htm"TITLE="17.20 grepping a Directory Tree (and a Gotcha) "><LINKREL="next"HREF="ch17_22.htm"TITLE="17.22 Finding the Links to a File "></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_20.htm"TITLE="17.20 grepping a Directory Tree (and a Gotcha) "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 17.20 grepping a Directory Tree (and a Gotcha) "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_22.htm"TITLE="17.22 Finding the Links to a File "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 17.22 Finding the Links to a File "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7030">17.21 lookfor: Which File Has that Word? </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-19700"></A><ACLASS="indexterm"NAME="AUTOID-19702"></A><ACLASS="indexterm"NAME="AUTOID-19706"></A>The following simple shell script, <EMCLASS="emphasis">lookfor</EM>, uses<SPANCLASS="link"><EMCLASS="emphasis">find</EM> (<ACLASS="linkend"HREF="ch17_01.htm#UPT-ART-7410"TITLE="The find Command Is Great; The Problem Is Finding How to Use It">17.1</A>)</SPAN>to lookfor all files in the specified directory hierarchy that have been modified within a certaintime, and it passes the resulting names to<SPANCLASS="link"><EMCLASS="emphasis">grep</EM> (<ACLASS="linkend"HREF="ch27_02.htm"TITLE="Searching for Text with grep ">27.2</A>)</SPAN>to scan for a particular pattern.For example, the command:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>lookfor /work -7 tamale enchilada</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">would search through the entire <EMCLASS="emphasis">/work</EM> filesystem and print the names ofall files modified withinthe past week that contain the words "tamale" or "enchilada".(So, for example:if this article is stored on <EMCLASS="emphasis">/work</EM>, <EMCLASS="emphasis">lookfor</EM> should find it.)</P><PCLASS="para">The arguments to the script are the pathnameof a directory hierarchy to search in (<CODECLASS="literal">$1</CODE>), a time (<CODECLASS="literal">$2</CODE>),and one or more text patterns (the other arguments).This simple but slow version will search for an (almost) unlimited number ofwords:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"> #!/bin/shtemp=/tmp/lookfor$$trap 'rm -f $temp; exit' 0 1 2 15find $1 -mtime $2 -print > $tempshift; shiftfor worddo grep -i "$word" `cat $temp` /dev/nulldone</PRE></BLOCKQUOTE></P><PCLASS="para">That version runs <EMCLASS="emphasis">grep</EM> once to search for each word.<ACLASS="indexterm"NAME="AUTOID-19729"></A>The <EMCLASS="emphasis">-i</EM> option makes the search find either uppercase or lowercaseletters.Using <EMCLASS="emphasis">/dev/null</EM><SPANCLASS="link"> makes sure that <EMCLASS="emphasis">grep</EM> will print the filename . (<ACLASS="linkend"HREF="ch13_14.htm"TITLE="What Can You Do with an Empty File? ">13.14</A>)</SPAN>Watch out: the list of filenames may get<SPANCLASS="link">too long (<ACLASS="linkend"HREF="ch09_20.htm"TITLE="Too Many Files for the Command Line ">9.20</A>)</SPAN>.</P><PCLASS="para">The next version is more limited but faster.It builds a regular expression for<ACLASS="indexterm"NAME="AUTOID-19738"></A><ACLASS="indexterm"NAME="AUTOID-19740"></A><SPANCLASS="link"><EMCLASS="emphasis">egrep</EM> (<ACLASS="linkend"HREF="ch27_05.htm"TITLE="Extended Searching for Text with egrep ">27.5</A>)</SPAN>that finds all the words in one pass through the files.If you use too many words, <EMCLASS="emphasis">egrep</EM> will say <CODECLASS="literal">Regular expression too long</CODE>.Your <EMCLASS="emphasis">egrep</EM> may not have a <EMCLASS="emphasis">-i</EM> option; you can just omit it.This version also uses<SPANCLASS="link"><EMCLASS="emphasis">xargs</EM> (<ACLASS="linkend"HREF="ch09_21.htm"TITLE="Handle Too-Long Command Lines with xargs ">9.21</A>)</SPAN>;though <EMCLASS="emphasis">xargs</EM> has its<SPANCLASS="link">problems (<ACLASS="linkend"HREF="ch09_22.htm"TITLE="xargs: Problems with Spaces and Newlines ">9.22</A>)</SPAN>.</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"> #!/bin/shwhere="$1"when="$2"shift; shift# Build egrep expression like (word1|word2|...) in $exprfor worddo case "$expr" in "") expr="($word" ;; *) expr="$expr|$word" ;; esacdoneexpr="$expr)"find $where -mtime $when -print | xargs egrep -i "$expr" /dev/null</PRE></BLOCKQUOTE></P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</SPAN>, <SPANCLASS="authorinitials">TOR</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_20.htm"TITLE="17.20 grepping a Directory Tree (and a Gotcha) "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 17.20 grepping a Directory Tree (and a Gotcha) "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_22.htm"TITLE="17.22 Finding the Links to a File "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 17.22 Finding the Links to a File "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">17.20 grepping a Directory Tree (and a Gotcha) </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.22 Finding the Links to a File </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 + -