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

📄 ch23_18.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 23] 23.18 How Making and Deleting Directories Works </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:41:15Z"><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_17.htm"TITLE="23.17 Problems Deleting Directories "><LINKREL="next"HREF="ch23_19.htm"TITLE="23.19 Deleting (BSD) Manual Pages that Aren't Read "></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_17.htm"TITLE="23.17 Problems Deleting Directories "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 23.17 Problems Deleting Directories "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_19.htm"TITLE="23.19 Deleting (BSD) Manual Pages that Aren't Read "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 23.19 Deleting (BSD) Manual Pages that Aren't Read "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-0104">23.18 How Making and Deleting Directories Works </A></H2><PCLASS="para"></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-25904"></A><ACLASS="indexterm"NAME="AUTOID-25907"></A>Every file in the UNIX filesystem&nbsp;- and a directory is just afile, albeit a rather special one&nbsp;- is represented by one<SPANCLASS="link"><EMCLASS="emphasis">inode</EM> (<ACLASS="linkend"HREF="ch01_22.htm"TITLE="How UNIX Keeps Track of Files: Inodes ">1.22</A>)</SPAN>and one or more names (<SPANCLASS="link">directory entries (<ACLASS="linkend"HREF="ch18_02.htm"TITLE="What's Really in a Directory ">18.2</A>)</SPAN>).In a sense the inode<EMCLASS="emphasis">is</EM> the file; each name is a<SPANCLASS="link"><EMCLASS="emphasis">link</EM> (<ACLASS="linkend"HREF="ch18_04.htm"TITLE="More About Links ">18.4</A>)</SPAN>to this inode.An ordinaryfile may have anywhere from one to several thousand links (theexact limit is system dependent), but a directory never has anyfewer than two. Every directory has at least two names.</P><PCLASS="para">Suppose you start in <EMCLASS="emphasis">/usr/tmp</EM> and do a <EMCLASS="emphasis">mkdir&nbsp;x</EM>. What are thetwo links to <EMCLASS="emphasis">x</EM>? They are <EMCLASS="emphasis">/usr/tmp/x</EM> and <EMCLASS="emphasis">/usr/tmp/x/.</EM>,directory entries in <EMCLASS="emphasis">/usr/tmp</EM> and <EMCLASS="emphasis">/usr/tmp/x</EM>, respectively.This might seem rather odd at first: how can a directory nameitself? It's not hard: first you create <EMCLASS="emphasis">/usr/tmp/x</EM>, a completelyempty directory, then link <EMCLASS="emphasis">/usr/tmp/x</EM> to <EMCLASS="emphasis">/usr/tmp/x/.</EM>and you're halfway done.All<EMCLASS="emphasis">link</EM> does is take its first name and turn it into an inode&nbsp;- thefile itself&nbsp;- then make a new entry for the second name,pointing to that inode.You must also link <EMCLASS="emphasis">/usr/tmp</EM> to <EMCLASS="emphasis">/usr/tmp/x/..</EM> to make a properlyformed directory. The <EMCLASS="emphasis">mkdir</EM> program and system callboth do all this properly; and there is no other way for anyoneexcept the<SPANCLASS="link">superuser (<ACLASS="linkend"HREF="ch01_24.htm"TITLE="The Superuser (Root) ">1.24</A>)</SPAN>to create a directory.</P><PCLASS="para">Here is where the trouble creeps in. All <EMCLASS="emphasis">unlink</EM>(2) does is takethe name it is given, convert it to an inode, and remove the name.If the name was the last link to that inode, the file itself isdestroyed as well; if not, it is left intact and may still beaccessed by its other name(s). So what happens if you unlink adirectory? Well, if it is completely empty, it goes away andeverything is fine. However, if it still has <CODECLASS="literal">.</CODE> and<CODECLASS="literal">..</CODE> in it&nbsp;- and it almost certainly will&nbsp;- things arenot so good. The <CODECLASS="literal">.</CODE> link to the directory itself stillexists, so the file that is the directory is not deleted. Thename <EMCLASS="emphasis">/usr/tmp/x</EM> <EMCLASS="emphasis">is</EM> deleted, and that leaves us with a prettyproblem: how can we get rid of that last <CODECLASS="literal">.</CODE> and <CODECLASS="literal">..</CODE>?</P><PCLASS="para">The answer is that we cannot. That directory will stick aroundforever. Worse, it has in it another name for, or link to, <EMCLASS="emphasis">/usr/tmp</EM>,which means that that, too, cannot be deleted. Of course, <EMCLASS="emphasis">fsck</EM>(which does not use the regular filesystem mechanisms) can cleanthis up, but this usually requires a system shutdown.[<EMCLASS="emphasis">fsck</EM> is a filesystem-checking program that the systemadministrator runs. <EMCLASS="emphasis">-JP</EM>&nbsp;]For this reason, again, only the superuser may unlink a directory.Ordinary processes must use the <EMCLASS="emphasis">rmdir</EM> program or system call.</P><PCLASS="para">Incidentally, the <EMCLASS="emphasis">mkdir</EM>(2) and <EMCLASS="emphasis">rmdir</EM>(2) system callsdo not exist on older UNIX systems. On these systems, you mustuse careful<SPANCLASS="link"><EMCLASS="emphasis">fork</EM>-<EMCLASS="emphasis">exec</EM> (<ACLASS="linkend"HREF="ch38_02.htm"TITLE="fork and exec ">38.2</A>)</SPAN>sequences to run the <EMCLASS="emphasis">mkdir</EM> and<EMCLASS="emphasis">rmdir</EM> programs.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">CT</SPAN> <SPANCLASS="bibliomisc">in <ACLASS="systemitem.newsgroup"HREF="news:net.unix">net.unix</A> on Usenet, 25 July 1986</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_17.htm"TITLE="23.17 Problems Deleting Directories "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 23.17 Problems Deleting Directories "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_19.htm"TITLE="23.19 Deleting (BSD) Manual Pages that Aren't Read "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 23.19 Deleting (BSD) Manual Pages that Aren't Read "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">23.17 Problems Deleting Directories </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.19 Deleting (BSD) Manual Pages that Aren't Read </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 + -