📄 ch14_04.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 14] 14.4 How Does UNIX Find Your Current 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:37:01Z"><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="ch14_01.htm"TITLE="14. Moving Around in a Hurry"><LINKREL="prev"HREF="ch14_03.htm"TITLE="14.3 What Good Is a Current Directory? "><LINKREL="next"HREF="ch14_05.htm"TITLE="14.5 Saving Time When You Change Directories: cdpath "></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="ch14_03.htm"TITLE="14.3 What Good Is a Current Directory? "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 14.3 What Good Is a Current Directory? "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 14<BR>Moving Around in a Hurry</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch14_05.htm"TITLE="14.5 Saving Time When You Change Directories: cdpath "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 14.5 Saving Time When You Change Directories: cdpath "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7934">14.4 How Does UNIX Find Your Current Directory? </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-15213"></A><ACLASS="indexterm"NAME="AUTOID-15216"></A>A command like <EMCLASS="emphasis">pwd</EM> inherits the current directory of the processthat started it (usually, a shell).It could be started from anywhere.How does <EMCLASS="emphasis">pwd</EM> find out where it is in the filesystem?See<ACLASS="xref"HREF="ch14_04.htm#UPT-ART-7934-FIG-0"TITLE="Finding the Current Directory Name">Figure 14.2</A>for a picture of the current directory <EMCLASS="emphasis">/usr/joe</EM> and its parentdirectories.<ACLASS="indexterm"NAME="AUTOID-15223"></A>The current directory doesn't contain its own name, so that doesn't help <EMCLASS="emphasis">pwd</EM>.But it has an entry named <CODECLASS="literal">.</CODE> (dot), which gives the <SPANCLASS="link"> i-number ofthe directory . (<ACLASS="linkend"HREF="ch18_02.htm"TITLE="What's Really in a Directory ">18.2</A>)</SPAN></P><H4CLASS="figure"><ACLASS="title"NAME="UPT-ART-7934-FIG-0">Figure 14.2: Finding the Current Directory Name</A></H4><IMGCLASS="graphic"SRC="figs/7934.gif"ALT="Figure 14.2"><PCLASS="para">The current directory has i-number 234.Next, <EMCLASS="emphasis">pwd</EM> asks UNIX to open the parent directory file,the directory one level up,through the relative pathname (<CODECLASS="literal">..</CODE>).It's looking for the name that goes with i-number 234.Aha; the current directory is named <EMCLASS="emphasis">joe</EM>.So the end of the pathname must be <EMCLASS="emphasis">joe</EM>.</P><PCLASS="para">Next step.<EMCLASS="emphasis">pwd</EM> looks at the <CODECLASS="literal">.</CODE> entry in the directory one level up toget its i-number: 14.Like always, the name of the one-level-up directory is in its parent(<CODECLASS="literal">..</CODE>, i-number 12).To get its name, <EMCLASS="emphasis">pwd</EM> opens the directory two levels upand looks for i-number 14: <EMCLASS="emphasis">usr</EM>.Now <EMCLASS="emphasis">pwd</EM> has the pathname <EMCLASS="emphasis">usr/joe</EM>.</P><PCLASS="para">Same steps: look in the parent, i-number 12.What's its name?Hmmm.The i-number of its parent, 12, is the same as its own-and there's onlyone directory on the filesystem like this: the root directory.So, <EMCLASS="emphasis">pwd</EM> adds a slash to the start of the pathname and it's done:<EMCLASS="emphasis">/usr/joe</EM>.</P><PCLASS="para">That's really missing one or two parts: Filesystems can be mounted on otherfilesystems, or can be mounted across the network from other hosts.So, at each step, <EMCLASS="emphasis">pwd</EM> also needs to check the device that the currentdirectory is mounted on.If you're curious, see the <EMCLASS="emphasis">stat</EM>(2) manual page or check a UNIXinternals book.Also see the last few paragraphs of article<ACLASS="xref"HREF="ch18_04.htm"TITLE="More About Links ">18.4</A>for more about the links between directories.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</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="ch14_03.htm"TITLE="14.3 What Good Is a Current Directory? "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 14.3 What Good Is a Current Directory? "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="ch14_05.htm"TITLE="14.5 Saving Time When You Change Directories: cdpath "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 14.5 Saving Time When You Change Directories: cdpath "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">14.3 What Good Is a Current Directory? </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">14.5 Saving Time When You Change Directories: cdpath </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 + -