📄 ch14_05.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 14] 14.5 Saving Time When You Change Directories: cdpath </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:02Z"><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_04.htm"TITLE="14.4 How Does UNIX Find Your Current Directory? "><LINKREL="next"HREF="ch14_06.htm"TITLE="14.6 The Shells' pushd and popd Commands "></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_04.htm"TITLE="14.4 How Does UNIX Find Your Current Directory? "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 14.4 How Does UNIX Find Your 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_06.htm"TITLE="14.6 The Shells' pushd and popd Commands "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 14.6 The Shells' pushd and popd Commands "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1380">14.5 Saving Time When You Change Directories: cdpath </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="UPT-ART-1380-IX-DIRECTORIES-CHANGING"></A><ACLASS="indexterm"NAME="AUTOID-15259"></A><ACLASS="indexterm"NAME="AUTOID-15261"></A><ACLASS="indexterm"NAME="AUTOID-15263"></A>Some people make a shell<SPANCLASS="link">alias (<ACLASS="linkend"HREF="ch10_02.htm"TITLE="Aliases for Common Commands ">10.2</A>)</SPAN>for directories they <EMCLASS="emphasis">cd</EM> to often.Other people set<SPANCLASS="link">shell variables (<ACLASS="linkend"HREF="ch06_08.htm"TITLE="Shell Variables ">6.8</A>)</SPAN>to hold the pathnames of directoriesthey don't want to retype.<ACLASS="indexterm"NAME="AUTOID-15269"></A>But both of those methods make you remember directory abbreviations - andmake you put new aliases or shell variables in <EMCLASS="emphasis">.cshrc</EM> or<EMCLASS="emphasis">.profile</EM>each time you want to add or change one.There's an easier way: the C shell's <EMCLASS="emphasis">cdpath</EM> shell variableand the <EMCLASS="emphasis">CDPATH</EM> variable in <EMCLASS="emphasis">ksh</EM>, <EMCLASS="emphasis">bash</EM>, and someversions of <EMCLASS="emphasis">sh</EM>.I'll use the term "cdpath" to talk about all shells.</P><PCLASS="para">When you type the command <CODECLASS="literal">cd</CODE> <CODECLASS="replaceable"><I>foo</I></CODE>, the shell first triesto go to the exact pathname <CODECLASS="replaceable"><I>foo</I></CODE>.If that doesn't work, and if <CODECLASS="replaceable"><I>foo</I></CODE> is a relative pathname,the shell tries the same command from every directory listed in the <EMCLASS="emphasis">cdpath</EM>.(If you use <EMCLASS="emphasis">ksh</EM> or <EMCLASS="emphasis">sh</EM>, see the note at the end of this article.)</P><PCLASS="para">Let's say that your home directory is <EMCLASS="emphasis">/home/lisa</EM> and your currentdirectory is somewhere else.Let's also say that your <EMCLASS="emphasis">cdpath</EM> has the directories <EMCLASS="emphasis">/home/lisa</EM>,<EMCLASS="emphasis">/home/lisa/projects</EM>, and <EMCLASS="emphasis">/books/troff</EM>.If your <CODECLASS="literal">cd</CODE> <CODECLASS="replaceable"><I>foo</I></CODE> command doesn't work in your currentdirectory, then your shell will try<CODECLASS="literal">cd /home/lisa/</CODE><CODECLASS="replaceable"><I>foo</I></CODE>,<CODECLASS="literal">cd /home/lisa/projects/</CODE><CODECLASS="replaceable"><I>foo</I></CODE>,and<CODECLASS="literal">cd /books/troff/</CODE><CODECLASS="replaceable"><I>foo</I></CODE>, in that order.If the shell finds one, it shows the pathname:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cd foo</B></CODE>/home/lisa/foo%</PRE></BLOCKQUOTE></P><PCLASS="para">Some Bourne shells don't show the directory name.All shells print an error, though, if they can't find any <CODECLASS="replaceable"><I>foo</I></CODE> directory.</P><PCLASS="para">So, set your <EMCLASS="emphasis">cdpath</EM> to a list of the parent directoriesthat contain directories you might want to <EMCLASS="emphasis">cd</EM> to.<EMCLASS="emphasis">Don't list the exact directories - list the</EM><SPANCLASS="link"><EMCLASS="emphasis">parent directories</EM> (<ACLASS="linkend"HREF="ch01_21.htm"TITLE="Making Pathnames ">1.21</A>)</SPAN>.This list goes in your <EMCLASS="emphasis">.cshrc</EM> or <EMCLASS="emphasis">.profile</EM> file.For example, <EMCLASS="emphasis">lisa</EM>'s <EMCLASS="emphasis">.cshrc</EM> could have:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch14_11.htm"TITLE="14.11 Finding (Anyone's) Home Directory, Quickly ">~</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">set cdpath=(~ ~/projects /books/troff)</PRE></TD></TR></TABLE></P><PCLASS="para">A Bourne shell user would have this in <EMCLASS="emphasis">.profile</EM>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">CDPATH=:$HOME:$HOME/projects:/books/troffexport CDPATH</PRE></BLOCKQUOTE></P><PCLASS="para">(If your system doesn't define <CODECLASS="literal">$HOME</CODE>, try <CODECLASS="literal">$LOGDIR</CODE>.)</P><BLOCKQUOTECLASS="note"><PCLASS="para"><STRONG>NOTE:</STRONG> Note that the Bourne shell <EMCLASS="emphasis">CDPATH</EM> in the above example starts with acolon (<CODECLASS="literal">:</CODE>)-which, as in the<EMCLASS="emphasis">PATH</EM> variable, is actually<SPANCLASS="link">an empty entry (<ACLASS="linkend"HREF="ch06_04.htm"TITLE="The PATH Environment Variable ">6.4</A>)</SPAN>that stands for "the current directory."Both the <EMCLASS="emphasis">sh</EM> and <EMCLASS="emphasis">ksh</EM> I tested required that.Without an empty entry, neither <EMCLASS="emphasis">sh</EM> or <EMCLASS="emphasis">ksh</EM> would<EMCLASS="emphasis">cd</EM> into the current directory!(<EMCLASS="emphasis">bash</EM> seemed to work like <EMCLASS="emphasis">csh</EM>, though.)You could actually call this a feature.If there's no empty entry in <EMCLASS="emphasis">CDPATH</EM>, a user has to use<CODECLASS="literal">cd </CODE><CODECLASS="replaceable"><I>./subdirname</I></CODE> to go to a subdirectory of the currentdirectory.</P></BLOCKQUOTE><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_04.htm"TITLE="14.4 How Does UNIX Find Your Current Directory? "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 14.4 How Does UNIX Find Your 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_06.htm"TITLE="14.6 The Shells' pushd and popd Commands "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 14.6 The Shells' pushd and popd Commands "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">14.4 How Does UNIX Find Your 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.6 The Shells' pushd and popd Commands </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 + -