📄 ch18_02.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 18] 18.2 What's Really in a Directory </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:51Z"><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="ch18_01.htm"TITLE="18. Linking, Renaming, and Copying Files"><LINKREL="prev"HREF="ch18_01.htm"TITLE="18.1 What's So Complicated About Copying Files? "><LINKREL="next"HREF="ch18_03.htm"TITLE="18.3 Files with Two or More Names"></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="ch18_01.htm"TITLE="18.1 What's So Complicated About Copying Files? "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 18.1 What's So Complicated About Copying Files? "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 18<BR>Linking, Renaming, and Copying Files</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch18_03.htm"TITLE="18.3 Files with Two or More Names"><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 18.3 Files with Two or More Names"BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1450">18.2 What's Really in a Directory </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-20082"></A>Before you can understand moving and copying files, you need to knowa bit more about how files are represented in directories.What does it mean to say that a file is really "in" a directory?It's easy to imagine that files are actually inside of something(some special chunk of the disk that's called a directory).But that's precisely wrong, and it's one place where the<SPANCLASS="link">filing cabinet model (<ACLASS="linkend"HREF="ch01_19.htm"TITLE="The Tree Structure of the Filesystem ">1.19</A>)</SPAN>of a filesystem doesn't apply.</P><PCLASS="para">A directory really is just another file, and really isn't differentfrom any other data file.If you want to prove this, try the command<ACLASS="indexterm"NAME="AUTOID-20087"></A><CODECLASS="literal">od -c .</CODE>; on many UNIX systems,it dumps the current directory to the screen in raw form.It will certainly look ugly (it's not a text file - it has lotsof binary characters).But, if your system allows it,<SPANCLASS="link"><EMCLASS="emphasis">od -c</EM> (<ACLASS="linkend"HREF="ch25_07.htm"TITLE="Show Non-Printing Characters with cat -v or od -c ">25.7</A>)</SPAN>should let you see the names of the files that are inthe current directory[and, probably, some names of files that have been deleted!Sorry, they're only the old directory entries;<SPANCLASS="link">you can't get the files back (<ACLASS="linkend"HREF="ch23_02.htm"TITLE="rm and Its Dangers ">23.2</A>)</SPAN>.<EMCLASS="emphasis">-JP</EM>].If <EMCLASS="emphasis">od -c</EM> doesn't work, use <CODECLASS="literal">ls -if</CODE> instead.</P><PCLASS="para">So a directory is really just a list of files. It contains filenamesand<SPANCLASS="link">inode numbers (<ACLASS="linkend"HREF="ch01_22.htm"TITLE="How UNIX Keeps Track of Files: Inodes ">1.22</A>)</SPAN>.That is, we can visualize a directory like this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">The file named . is inode 34346The file named .. is inode 987The file named mr.ed is inode 10674The file named joe.txt is inode 8767The file named grok is inode 67871The file named otherdir is inode 2345</PRE></BLOCKQUOTE></P><PCLASS="para">So when you give a filename like <EMCLASS="emphasis">grok</EM>, the kernel looks up<EMCLASS="emphasis">grok</EM> in the current directory and finds out that this file has inode67871; it looks up this inode to find out who owns the file,where the data blocks are, and so on.</P><PCLASS="para">What's more, some of these "files" may be directories in their ownright. In particular, that's true of the first two entries:<CODECLASS="literal">.</CODE>and <CODECLASS="literal">..</CODE>. These entries are in <EMCLASS="emphasis">every</EM> directory.Single <CODECLASS="literal">.</CODE> just refers to the current directory, while double <CODECLASS="literal">..</CODE>refers to the "parent" of the current directory (i.e., the directorythat "contains" the current directory). The file <EMCLASS="emphasis">otherdir</EM> isyet another directory that happens to be "within" the current directory.But there's no way you can tell that from its directory entry-UNIXdoesn't know it's different until it looks up its inode.</P><PCLASS="para">Now that you know what a directory is, let's think about some basicoperations. What does it mean to move, or rename, a file? If the<ACLASS="indexterm"NAME="AUTOID-20112"></A><ACLASS="indexterm"NAME="AUTOID-20114"></A><ACLASS="indexterm"NAME="AUTOID-20117"></A><ACLASS="indexterm"NAME="AUTOID-20119"></A><ACLASS="indexterm"NAME="AUTOID-20123"></A><ACLASS="indexterm"NAME="AUTOID-20126"></A><ACLASS="indexterm"NAME="AUTOID-20129"></A><ACLASS="indexterm"NAME="AUTOID-20131"></A>file is staying in the same directory, the <EMCLASS="emphasis">mv</EM> command just changes the file's name in the directory; it doesn't touch the data at all.</P><PCLASS="para">Moving a file into another directory takes a little more work, but notmuch. A command like <EMCLASS="emphasis">mv dir1/foo dir2/foo</EM> means "delete <EMCLASS="emphasis">foo</EM>'sentry in <EMCLASS="emphasis">dir1</EM>, and create a new entry for <EMCLASS="emphasis">foo</EM> in <EMCLASS="emphasis">dir2</EM>." Again, UNIX doesn't have to touch the data blocks or the inode at all.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-20142"></A>The only time you actually need to copy data is if you're moving afile into another filesystem. In that case, you have to copy thefile to the new filesystem; delete its old directory entry; return thefile's data blocks to the "free list," which means that they can be re-used; and so on.It's a fairly complicated operation, but (still) relatively rare.(On some old versions of UNIX, <EMCLASS="emphasis">mv</EM> won't let you move filesbetween filesystems.)</P><PCLASS="para">Now let's see if you've understood. How does UNIX find out the name ofthe current directory? In our "current directory," there's an entryfor <CODECLASS="literal">.</CODE>, which tells us that the current directory has inode34346. Is the directory's name part of the inode? Sorry - it isn't.The directory's name is included in the parent directory. Theparent directory is <CODECLASS="literal">..</CODE>, which is inode 987. So UNIX looks upinode 987, finds out where the data is, and starts reading every entryin the parent directory. Sooner or later, it will find one thatcorresponds to inode 34346. When it does that, it knows that it has found thedirectory entry for the current directory, and can read its name.Article<ACLASS="xref"HREF="ch14_04.htm"TITLE="How Does UNIX Find Your Current Directory? ">14.4</A>has a diagram and more explanation.</P><PCLASS="para">Complicated? Yes, but if you understand this, you have a pretty goodidea of how UNIX directories work.</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="ch18_01.htm"TITLE="18.1 What's So Complicated About Copying Files? "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 18.1 What's So Complicated About Copying Files? "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="ch18_03.htm"TITLE="18.3 Files with Two or More Names"><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 18.3 Files with Two or More Names"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">18.1 What's So Complicated About Copying Files? </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">18.3 Files with Two or More Names</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 + -