📄 ch01_21.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 1] 1.21 Making Pathnames </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:31:25Z"><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="ch01_01.htm"TITLE="1. Introduction"><LINKREL="prev"HREF="ch01_20.htm"TITLE="1.20 Your Home Directory "><LINKREL="next"HREF="ch01_22.htm"TITLE="1.22 How UNIX Keeps Track of Files: Inodes "></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="ch01_20.htm"TITLE="1.20 Your Home Directory "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 1.20 Your Home Directory "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 1<BR>Introduction</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch01_22.htm"TITLE="1.22 How UNIX Keeps Track of Files: Inodes "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 1.22 How UNIX Keeps Track of Files: Inodes "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1022">1.21 Making Pathnames </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="UPT-ART-1022-IX-PATHNAMES-CREATING"></A>Pathnames locate a file (or directory, or any other object) in the UNIXfilesystem.As you read this article, refer to<ACLASS="xref"HREF="ch01_21.htm#UPT-ART-1022-FIG-0"TITLE="Part of a UNIX Filesystem Tree">Figure 1.4</A>.It's a diagram of a (very) small part of a UNIX filesystem.</P><H4CLASS="figure"><ACLASS="title"NAME="UPT-ART-1022-FIG-0">Figure 1.4: Part of a UNIX Filesystem Tree</A></H4><IMGCLASS="graphic"SRC="figs/1022.gif"ALT="Figure 1.4"><ACLASS="indexterm"NAME="AUTOID-1928"></A><ACLASS="indexterm"NAME="AUTOID-1930"></A><ACLASS="indexterm"NAME="AUTOID-1932"></A><PCLASS="para">Whenever you are using UNIX, you have a <EMCLASS="emphasis">current directory</EM>. Bydefault, UNIX looks for any files or directories that you mentionwithin the current directory. That is, if you don't give an absolutepathname (starting from the root, <EMCLASS="emphasis">/</EM> ), UNIX tries to look up files<EMCLASS="emphasis">relative</EM> to the current directory. When you first log in, yourcurrent directory is your<SPANCLASS="link">home directory (<ACLASS="linkend"HREF="ch01_20.htm"TITLE="Your Home Directory ">1.20</A>)</SPAN>,which the systemadministrator will assign to you. It typically has a name like<EMCLASS="emphasis">/usr/mike</EM> or <EMCLASS="emphasis">/home/mike</EM>. You can change your currentdirectory by giving the <EMCLASS="emphasis">cd</EM> command, followed by the name of anew directory (for example, <CODECLASS="literal">cd /usr/bin</CODE>). You can find out yourcurrent directory by giving the <EMCLASS="emphasis">pwd</EM> ("print working directory") command.</P><PCLASS="para">If your current directory is <EMCLASS="emphasis">/home/mike</EM>, andyou give the command <EMCLASS="emphasis">cat textfile</EM>, you are asking UNIX to locate thefile <EMCLASS="emphasis">textfile</EM> within the directory <EMCLASS="emphasis">/home/mike</EM>. This isequivalent to the absolute path <EMCLASS="emphasis">/home/mike/textfile</EM>. If you give thecommand <EMCLASS="emphasis">cat notes/textfile</EM>, you are asking UNIX to locate the file<EMCLASS="emphasis">textfile</EM> within the directory <EMCLASS="emphasis">notes</EM>, withinthe current directory <EMCLASS="emphasis">/home/mike</EM>.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-1957"></A>A number of abbreviations help you to form relative pathnames moreconveniently. You can use the abbreviation <CODECLASS="literal">.</CODE> (dot) to refer tothe current working directory. You can use <CODECLASS="literal">..</CODE> (dot dot) torefer to the parent of the current working directory. For example, ifyour current directory is <EMCLASS="emphasis">/home/mike</EM>, <EMCLASS="emphasis">./textfile</EM> is thesame as <EMCLASS="emphasis">textfile</EM>, which is the same as <EMCLASS="emphasis">/home/mike/textfile</EM>.The relative path <EMCLASS="emphasis">../gina/textfile</EM> is the same as<EMCLASS="emphasis">/home/gina/textfile</EM>; <CODECLASS="literal">..</CODE> moves up one level from<EMCLASS="emphasis">/home/mike</EM> (to <EMCLASS="emphasis">/home</EM>), and then searches for the directory<EMCLASS="emphasis">gina</EM> and the file <EMCLASS="emphasis">textfile</EM>.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-1974"></A>In the C shell, <EMCLASS="emphasis">ksh</EM> and <EMCLASS="emphasis">bash</EM>, you can use theabbreviation <CODECLASS="literal">~</CODE> (tilde) to referto your home directory.<CODECLASS="literal">~</CODE><CODECLASS="replaceable"><I>name</I></CODE> refers to the home directory of the user <EMCLASS="emphasis">name</EM>.See article<ACLASS="xref"HREF="ch14_11.htm"TITLE="Finding (Anyone's) Home Directory, Quickly ">14.11</A>.</P><PCLASS="para">Here's a summary of the rules that UNIX uses to interpret paths:</P><DLCLASS="variablelist"><DTCLASS="term"><EMCLASS="emphasis">If the pathname begins with</EM> <CODECLASS="literal">/</CODE></DT><DDCLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-1992"></A>It is an absolute path, starting from the root.</P></DD><DTCLASS="term"><EMCLASS="emphasis">If the pathname begins with</EM> <CODECLASS="literal">~</CODE> <EMCLASS="emphasis">or with</EM> <CODECLASS="literal">~</CODE><CODECLASS="replaceable"><I>name</I></CODE></DT><DDCLASS="listitem"><PCLASS="para">The C shell, <EMCLASS="emphasis">ksh</EM> and <EMCLASS="emphasis">bash</EM> turn it into an absolute pathnamestarting at your home directory (<CODECLASS="literal">~</CODE>), or at the home directory of theuser <EMCLASS="emphasis">name</EM> (<CODECLASS="literal">~</CODE><CODECLASS="replaceable"><I>name</I></CODE>).</P></DD><DTCLASS="term"><EMCLASS="emphasis">If the pathname does not begin with a</EM> <CODECLASS="literal">/</CODE></DT><DDCLASS="listitem"><PCLASS="para">The pathname is relative to the current directory.Two relative special cases use entries that are in every UNIX directory:</P><OLCLASS="orderedlist"><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-2019"></A><ACLASS="indexterm"NAME="AUTOID-2022"></A><ACLASS="indexterm"NAME="AUTOID-2024"></A>If the pathname begins with <CODECLASS="literal">./</CODE> - the path is relative to the current directory; for example,<EMCLASS="emphasis">./textfile</EM>.</P></LI><LICLASS="listitem"><PCLASS="para">If the pathname begins with <CODECLASS="literal">../</CODE> - the path is relative to the parent of the current directory. Forexample, if your current directory is <EMCLASS="emphasis">/home/mike/work</EM>,then <EMCLASS="emphasis">../src</EM> means <EMCLASS="emphasis">/home/mike/src</EM>.</P></LI></OL></DD></DL><PCLASS="para">Article <ACLASS="xref"HREF="ch18_02.htm"TITLE="What's Really in a Directory ">18.2</A> explains where <CODECLASS="literal">.</CODE> and <CODECLASS="literal">..</CODE> come from.</P><BLOCKQUOTECLASS="note"><PCLASS="para"><STRONG>NOTE:</STRONG> The <CODECLASS="literal">.</CODE> and <CODECLASS="literal">..</CODE> may appear at any point within a path. Theymean "the current directory at this point in the path" and "theparent of the current directory at this point in the path."You commonly see paths starting with <CODECLASS="literal">../../</CODE> (or more) to referto the grandparent or great-grandparent of the current directory.However, theycan appear at other places in a pathname as well.For example, <EMCLASS="emphasis">/usr/ucb/./bin</EM> is the same as <EMCLASS="emphasis">/usr/ucb/bin</EM>; and<EMCLASS="emphasis">/usr/ucb/bin/../lib</EM> is the same as <EMCLASS="emphasis">/usr/ucb/lib</EM>.Placing <CODECLASS="literal">.</CODE> or <CODECLASS="literal">..</CODE> in the middle of a path may be helpful inbuilding paths within shell scripts, but I have never seen them usedin any other useful way.</P></BLOCKQUOTE><ACLASS="indexterm"NAME="AUTOID-2049"></A><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">ML</SPAN>, <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="ch01_20.htm"TITLE="1.20 Your Home Directory "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 1.20 Your Home 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="ch01_22.htm"TITLE="1.22 How UNIX Keeps Track of Files: Inodes "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 1.22 How UNIX Keeps Track of Files: Inodes "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">1.20 Your Home 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">1.22 How UNIX Keeps Track of Files: Inodes </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 + -