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

📄 ch09_09.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 9.8. Removing a Directory and Its Contents (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:39:19Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-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="ch09_01.htm"TITLE="9. Directories"><LINKREL="prev"HREF="ch09_08.htm"TITLE="9.7. Processing All Files in a Directory Recursively"><LINKREL="next"HREF="ch09_10.htm"TITLE="9.9. Renaming Files"></HEAD><BODYBGCOLOR="#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="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch09_08.htm"TITLE="9.7. Processing All Files in a Directory Recursively"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 9.7. Processing All Files in a Directory Recursively"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch09_01.htm"TITLE="9. Directories"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch09_10.htm"TITLE="9.9. Renaming Files"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 9.9. Renaming Files"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch09-chap09_removing_0">9.8. Removing a Directory and Its Contents</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch09-pgfId-866">Problem<ACLASS="indexterm"NAME="ch09-idx-1000004419-0"></A><ACLASS="indexterm"NAME="ch09-idx-1000004419-1"></A></A></H3><PCLASS="para">You want to remove a directory tree recursively without using <CODECLASS="literal">rm</CODE> <CODECLASS="literal">-r</CODE>.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch09-pgfId-872">Solution</A></H3><PCLASS="para">Use the <CODECLASS="literal">finddepth</CODE><ACLASS="indexterm"NAME="ch09-idx-1000004637-0"></A><ACLASS="indexterm"NAME="ch09-idx-1000004637-1"></A> function from File::Find, shown in <ACLASS="xref"HREF="ch09_09.htm#ch09-26212"TITLE="rmtree1">Example 9.3</A>.</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch09-26212">Example 9.3: rmtree1</A></H4><PRECLASS="programlisting">#!/usr/bin/perl# rmtree1 - remove whole directory trees like rm -ruse File::Find qw(finddepth);die &quot;usage: $0 dir ..\n&quot; unless @ARGV;*name = *File::Find::name;finddepth \&amp;zap, @ARGV;sub zap {    if (!-l &amp;&amp; -d _) {        print &quot;rmdir $name\n&quot;;        rmdir($name)  or warn &quot;couldn't rmdir $name: $!&quot;;    } else {        print &quot;unlink $name&quot;;        unlink($name) or warn &quot;couldn't unlink $name: $!&quot;;    }}</PRE></DIV><PCLASS="para">Or use <CODECLASS="literal">rmtree</CODE> from File::Path, as shown in <ACLASS="xref"HREF="ch09_09.htm#ch09-11947"TITLE="rmtree2">Example 9.4</A>.</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch09-11947">Example 9.4: rmtree2</A></H4><PRECLASS="programlisting">#!/usr/bin/perl# <ACLASS="indexterm"NAME="ch09-idx-1000004491-0"></A>rmtree2 - remove whole directory trees like rm -ruse File::Path;die &quot;usage: $0 dir ..\n&quot; unless @ARGV;    foreach $dir (@ARGV) {    rmtree($dir);}</PRE></DIV><BLOCKQUOTECLASS="warning"><PCLASS="para"><STRONG>WARNING:</STRONG> These programs remove an entire directory tree. Use with extreme caution! <ACLASS="indexterm"NAME="ch09-idx-1000005735-0"></A><ACLASS="indexterm"NAME="ch09-idx-1000005735-1"></A><ACLASS="indexterm"NAME="ch09-idx-1000005735-2"></A></P></BLOCKQUOTE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch09-pgfId-922">Discussion</A></H3><PCLASS="para">The File::Find module exports both a <CODECLASS="literal">find</CODE> function, which traverses a tree in the (essentially random) order the files occur in the directory, as well as a <CODECLASS="literal">finddepth</CODE> function, which is guaranteed to visit all the files underneath a directory before visiting the directory itself. This is exactly what we need to remove a directory and its contents.</P><PCLASS="para">We have to use two different functions, <CODECLASS="literal">rmdir</CODE><ACLASS="indexterm"NAME="ch09-idx-1000004421-0"></A><ACLASS="indexterm"NAME="ch09-idx-1000004421-1"></A> and <CODECLASS="literal">unlink</CODE>. The <CODECLASS="literal">unlink</CODE> function deletes only files, and <CODECLASS="literal">rmdir</CODE> only deletes empty directories. We need to use <CODECLASS="literal">finddepth</CODE> to make sure that we've first removed the directory's contents before we <CODECLASS="literal">rmdir</CODE> the directory itself.</P><PCLASS="para">Check first that the file isn't a symbolic link before determining if it's a directory. <CODECLASS="literal">-d</CODE> returns true for both a directory and a symbol link to a directory. <CODECLASS="literal">stat</CODE>, <CODECLASS="literal">lstat</CODE>, and the file test operators like <CODECLASS="literal">-d</CODE> all use the operating system call <EMCLASS="emphasis">stat </EM>(2), which returns all the information kept about a file in an inode. These functions and operators retain that information and let you do more tests on the same file with the special underscore ( <CODECLASS="literal">_  </CODE>) filehandle. This avoids redundant system calls that would return the same information, slowly.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch09-pgfId-934">See Also</A></H3><PCLASS="para">The <CODECLASS="literal">unlink </CODE>, <CODECLASS="literal">rmdir</CODE>, <CODECLASS="literal">lstat</CODE>, and <CODECLASS="literal">stat</CODE> functions in <ICLASS="filename">perlfunc</I> (1) and in <ACLASS="olink"HREF="../prog/ch03_01.htm">Chapter 3</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>; the documentation for the standard File::Find module; your system's <ICLASS="filename">rm </I>(1) and <ICLASS="filename">stat </I>(2) manpages; the <CODECLASS="literal">-</CODE>X section of <ICLASS="filename">perlfunc </I>(1), and the <ACLASS="olink"HREF="../prog/ch02_05.htm#PERL2-CH-2-SECT-5.10">"Named Unary and File Test Operators"</A> section of <ACLASS="olink"HREF="../prog/ch02_01.htm">Chapter 2</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch09_08.htm"TITLE="9.7. Processing All Files in a Directory Recursively"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 9.7. Processing All Files in a Directory Recursively"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch09_10.htm"TITLE="9.9. Renaming Files"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 9.9. Renaming Files"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">9.7. Processing All Files in a Directory Recursively</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">9.9. Renaming Files</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; 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 + -