📄 appb_26.htm
字号:
<html><head><title>Converting find Command Lines to Perl (Learning Perl, 3rd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Randal L. Schwartz and Tom Phoenix" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596001320L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Learning Perl, 3rd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Learning Perl, 3rd Edition" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="appb_25.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"></a></td><td align="right" valign="top" width="228"><a href="appb_27.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">B.26. Converting find Command Lines to Perl</h2><p>A common task for a system administrator is to recursively search thedirectory tree for certain items. On Unix, this is typically donewith the <i class="command">find</i><a name="INDEX-1324" /> command. We can do that directly fromPerl, too.</p><p>The <i class="command">find2perl</i><a name="INDEX-1325" /> command, which comes with Perl, takesthe same arguments that <i class="command">find</i> does. Instead offinding the requested items, however, the output of<i class="command">find2perl</i> is a Perl program that finds them. Sinceit's a program, you can edit it for your own needs. (Theprogram is written in a somewhat odd style.)</p><p>One useful argument that's available in<i class="command">find2perl</i> but not in the standard<i class="command">find</i> is the<tt class="literal">-eval</tt><a name="INDEX-1326" /> option. This says that what followsit is actual Perl code that should be run each time that a file isfound. When it's run, the current directory will be thedirectory in which some item is found, and <tt class="literal">$_</tt> willcontain the item's name.</p><p>Here's an example of how you might use<i class="command">find2perl</i>. Suppose that you're a systemadministrator on a Unix machine, and you want to find and remove allof the old <a name="INDEX-1327" />files in the <em class="filename">/tmp</em>directory.<a href="#FOOTNOTE-422">[422]</a> Here's the command that writes theprogram to do that:</p><blockquote class="footnote"> <a name="FOOTNOTE-422" /><p>[422]This is a task typically done by a<i class="command">cron </i>job at some early-morning hour eachday.</p> </blockquote><blockquote><pre class="code">$ <tt class="userinput"><b>find2perl /tmp -atime +14 -eval unlink >Perl-program</b></tt></pre></blockquote><p>That command says to search in <em class="filename">/tmp</em> (andrecursively in subdirectories) for items whose atime (last accesstime) is at least 14 days ago. For each item, the program should runthe Perl code <tt class="literal">unlink</tt>, which will use<tt class="literal">$_</tt> by default as the name of a file to remove. Theoutput (redirected to go into the file<em class="filename">Perl-program</em>) is the program that does all ofthis. Now you merely need to arrange for it to be run as needed.</p><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="appb_25.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img alt="Home" border="0" src="../gifs/txthome.gif" /></a></td><td align="right" valign="top" width="228"><a href="appb_27.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">B.25. Converting Other Languages to Perl</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img alt="Book Index" border="0" src="../gifs/index.gif" /></a></td><td align="right" valign="top" width="228">B.27. Command-line Options in Your Programs</td></tr></table></div><hr width="684" align="left" /><img alt="Library Navigation Links" border="0" src="../gifs/navbar.gif" usemap="#library-map" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -