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

📄 ch14_02.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 14] 14.2 Using Relative and Absolute Pathnames </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:36:59Z"><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_01.htm"TITLE="14.1 Getting Around the Filesystem "><LINKREL="next"HREF="ch14_03.htm"TITLE="14.3 What Good Is a Current Directory? "></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_01.htm"TITLE="14.1 Getting Around the Filesystem "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 14.1 Getting Around the Filesystem "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_03.htm"TITLE="14.3 What Good Is a Current Directory? "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 14.3 What Good Is a Current Directory? "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1360">14.2 Using Relative and Absolute Pathnames </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-15056"></A><ACLASS="indexterm"NAME="AUTOID-15058"></A><ACLASS="indexterm"NAME="AUTOID-15060"></A>Everything in the UNIX filesystem&nbsp;- files, directories, devices, named pipes,and so on&nbsp;- has two pathnames: absolute and relative.If you know how to find those names, you'll know the best way to locate thefile (or whatever) and use it.Even though pathnames are amazingly simple, they're one of the biggest problemsbeginners have.Studying this article carefully can save you a lot of time andfrustration. See<ACLASS="xref"HREF="ch14_02.htm#UPT-ART-1360-FIG-0"TITLE="A UNIX Filesystem Tree">Figure 14.1</A>for an illustration of the UNIX filesystem.</P><H4CLASS="figure"><ACLASS="title"NAME="UPT-ART-1360-FIG-0">Figure 14.1: A UNIX Filesystem Tree</A></H4><IMGCLASS="graphic"SRC="figs/1360.gif"ALT="Figure 14.1"><PCLASS="para"><ACLASS="xref"HREF="ch14_02.htm#UPT-ART-1360-TAB-0"TITLE="Absolute and Relative Pathnames">Table 14.1</A>describes the two kinds of pathnames.</P><TABLECLASS="table"><CAPTIONCLASS="table"><ACLASS="title"NAME="UPT-ART-1360-TAB-0">Table 14.1: Absolute and Relative Pathnames</A></CAPTION><THEADCLASS="thead"><TRCLASS="row"VALIGN="TOP"><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1">Absolute Pathnames</TH><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1">Relative Pathnames</TH></TR></THEAD><TBODYCLASS="tbody"><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">Start at the root directory.</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">Start at your <SPANCLASS="link">current directory (<ACLASS="linkend"HREF="ch01_21.htm"TITLE="Making Pathnames ">1.21</A>)</SPAN>.</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">Always start with a slash (<CODECLASS="literal">/</CODE>).</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">Never start with a slash.</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">The absolute pathname to some object (file, etc.) is always the same.</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">The relative pathname to an object depends on your current directory.</P></TD></TR></TBODY></TABLE><PCLASS="para">To make an absolute pathname:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">Start at the root directory (<CODECLASS="literal">/</CODE>) and work down.</P></LI><LICLASS="listitem"><PCLASS="para">Put a slash (<CODECLASS="literal">/</CODE>) after every directory name&nbsp;- though if the path endsat a directory, the slash after the last name is optional.</P></LI></UL><PCLASS="para">For example, to get a listing of the directory highlighted in<ACLASS="xref"HREF="ch14_02.htm#UPT-ART-1360-FIG-0"TITLE="A UNIX Filesystem Tree">Figure 14.1</A>,no matter what your current directory is,you'd use an absolute pathname like this:<ACLASS="indexterm"NAME="AUTOID-15104"></A></P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>ls /home/jane/data</B></CODE>Sub    a    b     c</PRE></BLOCKQUOTE></P><PCLASS="para">To make a relative pathname:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">Start at your current directory.</P></LI><LICLASS="listitem"><PCLASS="para">As you move down the tree, away from root, add subdirectory names.</P></LI><LICLASS="listitem"><PCLASS="para">As you move up the tree toward root, add <CODECLASS="literal">..</CODE> (two dots)for each directory.</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-15121"></A>Put a slash (<CODECLASS="literal">/</CODE>) after every directory name&nbsp;- though if the path isto a directory, the slash after the last name is optional.</P></LI></UL><PCLASS="para">For example, if your current directory is the one shown in<ACLASS="xref"HREF="ch14_02.htm#UPT-ART-1360-FIG-0"TITLE="A UNIX Filesystem Tree">Figure 14.1</A>,to get a listing of the <EMCLASS="emphasis">Sub</EM> subdirectory, use a relative pathname:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>ls Sub</B></CODE>d     e     f</PRE></BLOCKQUOTE></P><PCLASS="para">Without changing your current directory, you can use a relative pathnameto read the file <EMCLASS="emphasis">d</EM> in the <EMCLASS="emphasis">Sub</EM> subdirectory:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cat Sub/d</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">To change the current directory to Jim's home directory, you coulduse a relative pathname to it:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cd ../../jim</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">Using the absolute pathname, <EMCLASS="emphasis">/home/jim</EM>, might be easier there.</P><PCLASS="para">The<SPANCLASS="link">symbolic link (<ACLASS="linkend"HREF="ch18_04.htm"TITLE="More About Links ">18.4</A>)</SPAN>adds a twist to pathnames.What two absolute pathnames would read the file that the symlink pointsto?The answer: <EMCLASS="emphasis">/home/jane/.setup</EM> or <EMCLASS="emphasis">/work/setups/generic</EM>.(The second pathname points directly to the file, so it's a little moreefficient.)If your current directory was the one shown in<ACLASS="xref"HREF="ch14_02.htm#UPT-ART-1360-FIG-0"TITLE="A UNIX Filesystem Tree">Figure 14.1</A>,what would be the easiest way to read that file with the<SPANCLASS="link"><EMCLASS="emphasis">more</EM> (<ACLASS="linkend"HREF="ch25_03.htm"TITLE="Using more to Page Through Files ">25.3</A>)</SPAN>pager?It's probably through the symlink:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>more ../.setup</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">Remember, when you need to use something in the filesystem, you don't alwaysneed to use <EMCLASS="emphasis">cd</EM> first.Think about using a relative or absolute pathname with the command;that'll almost always work.If you get an error message, check your pathname carefully; that'salmost always the problem.If it's hard to visualizethe filesystem, a program that makes a<SPANCLASS="link">diagram of the directory tree (<ACLASS="linkend"HREF="ch16_19.htm"TITLE="stree: Simple Directory Tree ">16.19</A>, <ACLASS="linkend"HREF="ch16_20.htm"TITLE="The vtree Visual Directory Tree Programs ">16.20</A>)</SPAN>can help.</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_01.htm"TITLE="14.1 Getting Around the Filesystem "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 14.1 Getting Around the Filesystem "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_03.htm"TITLE="14.3 What Good Is a Current Directory? "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 14.3 What Good Is a Current Directory? "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">14.1 Getting Around the Filesystem </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.3 What Good Is a Current Directory? </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 + -