📄 ch23_17.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 23] 23.17 Problems Deleting Directories </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:41:14Z"><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="ch23_01.htm"TITLE="23. Removing Files"><LINKREL="prev"HREF="ch23_16.htm"TITLE="23.16 Removing a Strange File by its I-number "><LINKREL="next"HREF="ch23_18.htm"TITLE="23.18 How Making and Deleting Directories Works "></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="ch23_16.htm"TITLE="23.16 Removing a Strange File by its I-number "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 23.16 Removing a Strange File by its I-number "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 23<BR>Removing Files</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch23_18.htm"TITLE="23.18 How Making and Deleting Directories Works "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 23.18 How Making and Deleting Directories Works "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1750">23.17 Problems Deleting Directories </A></H2><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="rmdir">rmdir</A><BR></TH><TDVALIGN="TOP"><ACLASS="indexterm"NAME="AUTOID-25844"></A><ACLASS="indexterm"NAME="AUTOID-25847"></A><ACLASS="indexterm"NAME="AUTOID-25849"></A>What if you want to get rid of a directory?The standard way, and the safest way, to do this is to use theUNIX <EMCLASS="emphasis">rmdir</EM> "remove directory" utility (or the GNUversion on the CD-ROM):</TD></TR></TABLE><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>rmdir files</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">The <EMCLASS="emphasis">rmdir</EM> command often confuses new users. It will <EMCLASS="emphasis">only</EM>remove a directory if it is completely empty; otherwise, you'll getan error message:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>rmdir files</B></CODE>rmdir: files: Directory not empty% <CODECLASS="userinput"><B>ls files</B></CODE>%</PRE></BLOCKQUOTE></P><PCLASS="para">As in the example, <EMCLASS="emphasis">ls</EM> will often show that the directory isempty. What's going on?</P><PCLASS="para">It's common for editors and other programs to create "invisible" files(files with names beginning with a dot). The <EMCLASS="emphasis">ls</EM> commandnormally doesn't list them; if you want to see them, you have touse<SPANCLASS="link"><EMCLASS="emphasis">ls -a</EM> (<ACLASS="linkend"HREF="ch16_11.htm"TITLE="Showing Hidden Files with ls -A and -a ">16.11</A>)</SPAN>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>rmdir files</B></CODE>rmdir: files: Directory not empty% <CODECLASS="userinput"><B>ls -a files</B></CODE>. .. .BAK.textfile2</PRE></BLOCKQUOTE></P><PCLASS="para">Here, we see that the directory wasn't empty after all: there's abackup file that was left behind by some editor. You may have used<CODECLASS="literal">rm *</CODE> to clean the directory out, but that won't work: <EMCLASS="emphasis">rm</EM>also ignores files beginning with dots, unless you explicitly tell it todelete them.We really need a wildcard pattern like<SPANCLASS="link"><CODECLASS="literal">.??*</CODE> (or more) (<ACLASS="linkend"HREF="ch15_05.htm"TITLE='Matching All "Dot Files" with Wildcards'>15.5</A>)</SPAN>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>rmdir files</B></CODE>rmdir: files: Directory not empty% <CODECLASS="userinput"><B>ls -a files</B></CODE>. .. .BAK.textfile2% <CODECLASS="userinput"><B>rm files/.??*</B></CODE>% <CODECLASS="userinput"><B>rmdir files</B></CODE>%</PRE></BLOCKQUOTE></P><PCLASS="para">Other pitfalls might be files whose names consist of "non-printing"characters or blank spaces - sometimes these get created by accident orby malice (yes, some people think this is funny).Such files willusually give you<SPANCLASS="link">"suspicious" <EMCLASS="emphasis">ls</EM> output (<ACLASS="linkend"HREF="ch16_13.htm"TITLE="Can't Access a File? Look for Spaces in the Name ">16.13</A>)</SPAN>(like a blank line).</P><PCLASS="para">If you don't want to worry about all these special cases, just use<EMCLASS="emphasis">rm -r</EM>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>rm -r files</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">This command removes the directory and everything that's in it,including other directories. A lot of people warn you about it; it'sdangerous because it's easy to delete more than you realize.Personally, I use it all the time, and I've never made a mistake. I<EMCLASS="emphasis">never</EM> bother with <EMCLASS="emphasis">rmdir</EM>.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">ML</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="ch23_16.htm"TITLE="23.16 Removing a Strange File by its I-number "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 23.16 Removing a Strange File by its I-number "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="ch23_18.htm"TITLE="23.18 How Making and Deleting Directories Works "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 23.18 How Making and Deleting Directories Works "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">23.16 Removing a Strange File by its I-number </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">23.18 How Making and Deleting Directories Works </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 + -