📄 ch14_03.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 14] 14.3 What Good Is a 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:00Z"><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_02.htm"TITLE="14.2 Using Relative and Absolute Pathnames "><LINKREL="next"HREF="ch14_04.htm"TITLE="14.4 How Does UNIX Find Your 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_02.htm"TITLE="14.2 Using Relative and Absolute Pathnames "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 14.2 Using Relative and Absolute Pathnames "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_04.htm"TITLE="14.4 How Does UNIX Find Your Current Directory? "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 14.4 How Does UNIX Find Your Current Directory? "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1370">14.3 What Good Is a Current Directory? </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-15161"></A><ACLASS="indexterm"NAME="AUTOID-15164"></A><ACLASS="indexterm"NAME="AUTOID-15167"></A>People who think the <EMCLASS="emphasis">cd</EM> command is all they need to know about currentdirectories should read this article!Understanding how UNIX uses the current directory can save you work.</P><PCLASS="para">Each UNIX process has its own current directory.For instance, your shell has a current directory.So do <EMCLASS="emphasis">vi</EM>, <EMCLASS="emphasis">ls</EM>, <EMCLASS="emphasis">sed</EM>, and every other UNIX process.When your shell starts a process running, that child process startswith the same current directory as its parent.So how does <EMCLASS="emphasis">ls</EM> know which directory to list?It uses the current directory it inherited from its parent process, the shell:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>ls</B></CODE> ...<EMCLASS="emphasis">Listing of ls's current directory appears, which is the same current directory as the shell</EM>.</PRE></BLOCKQUOTE></P><PCLASS="para">Each process can change its current directory and that won't changethe current directory of other processes that are already running.So:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">Your shell script (which runs in a separate process) can <EMCLASS="emphasis">cd</EM>to another directory without affecting the shell that started it (the scriptdoesn't need to <EMCLASS="emphasis">cd</EM> back before it exits).</P></LI><LICLASS="listitem"><PCLASS="para">If you have more than one window or login session to the same computer,they probably run separate processes.So, they have independent current directories.</P></LI><LICLASS="listitem"><PCLASS="para">When you use a<SPANCLASS="link">subshell (<ACLASS="linkend"HREF="ch13_07.htm"TITLE="The () Subshell Operators ">13.7</A>, <ACLASS="linkend"HREF="ch38_04.htm"TITLE="Subshells ">38.4</A>)</SPAN>or a shell escape, you can <EMCLASS="emphasis">cd</EM> anywhere youwant.After you exit that shell, the parent shell's current directory won't havechanged.For example, if you want to run a command in another directory without<EMCLASS="emphasis">cd</EM>ing there first (and having to <EMCLASS="emphasis">cd</EM> back), do it in a subshell:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>pwd</B></CODE>/foo/bar% <CODECLASS="userinput"><B>(cd <CODECLASS="replaceable"><I>baz</I></CODE>; <CODECLASS="replaceable"><I>somecommand </I></CODE>> <CODECLASS="replaceable"><I>somefile</I></CODE>)</B></CODE>% <CODECLASS="userinput"><B>pwd</B></CODE>/foo/bar</PRE></BLOCKQUOTE></P></LI></UL><PCLASS="para">When you really get down to it, what good is a current directory?Here it is:<ACLASS="indexterm"NAME="AUTOID-15203"></A>relative pathnames start at the current directory.Having a current directory means you can refer to a file by itsrelative pathname, like <EMCLASS="emphasis">afile</EM>.Without a current directory and relative pathnames, you'd always have to use<SPANCLASS="link">absolute pathnames (<ACLASS="linkend"HREF="ch14_02.htm"TITLE="Using Relative and Absolute Pathnames ">14.2</A>)</SPAN>like <EMCLASS="emphasis">/usr/joe/projects/alpha/afile</EM>.</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_02.htm"TITLE="14.2 Using Relative and Absolute Pathnames "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 14.2 Using Relative and Absolute Pathnames "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_04.htm"TITLE="14.4 How Does UNIX Find Your Current Directory? "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 14.4 How Does UNIX Find Your Current Directory? "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">14.2 Using Relative and Absolute Pathnames </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.4 How Does UNIX Find Your 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 & 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 + -