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

📄 ch14_06.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 14] 14.6 The Shells' pushd and popd Commands </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:37:03Z"><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_05.htm"TITLE="14.5 Saving Time When You Change Directories: cdpath "><LINKREL="next"HREF="ch14_07.htm"TITLE="14.7 Nice Aliases for pushd "></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_05.htm"TITLE="14.5 Saving Time When You Change Directories: cdpath "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 14.5 Saving Time When You Change Directories: cdpath "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_07.htm"TITLE="14.7 Nice Aliases for pushd "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 14.7 Nice Aliases for pushd "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1390">14.6 The Shells' pushd and popd Commands </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-15346"></A><ACLASS="indexterm"NAME="AUTOID-15348"></A><ACLASS="indexterm"NAME="AUTOID-15350"></A>How often do you need to move to some other directory temporarily,look at some file, and then move back to the directory where youstarted?If you're like most users, you do this all the time.<EMCLASS="emphasis">csh</EM> and <EMCLASS="emphasis">bash</EM> have<EMCLASS="emphasis">pushd</EM> and <EMCLASS="emphasis">popd</EM> commands make this a lot easier.(If you use <EMCLASS="emphasis">ksh</EM>, O'Reilly &amp;amp; Associates' <EMCLASS="emphasis">Learning the KornShell</EM> shows you shell functions that do the same thing.)</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-15359"></A><ACLASS="indexterm"NAME="AUTOID-15362"></A>These commands implement a &quot;directory stack.&quot;The classicalanalogy for a stack is one of those spring-loaded plate stackers in aschool cafeteria.The last plate put (&quot;pushed&quot;) onto the stack is thefirst plate taken (&quot;popped&quot;) from the stack.It's just the same withdirectories: each time you use <EMCLASS="emphasis">pushd</EM>, the shell adds yourcurrent directory to the stack and moves you to the new directory.When youuse <EMCLASS="emphasis">popd</EM>, the shell takes the top directory off the stack,and moves you to the directory underneath.</P><PCLASS="para">You may as well learn about <EMCLASS="emphasis">pushd</EM> the way I did: by watching.Let's say that I'm in the directory <EMCLASS="emphasis">~/power</EM>, working on thisbook. I want to change to my <EMCLASS="emphasis">Mail</EM> directory briefly, to look atsome old correspondence.Here's how:[1]</P><BLOCKQUOTECLASS="footnote"><PCLASS="para">[1] If you've set a<SPANCLASS="link">cdpath (<ACLASS="linkend"HREF="ch14_05.htm"TITLE="Saving Time When You Change Directories: cdpath ">14.5</A>)</SPAN>you can use those short directory names with <EMCLASS="emphasis">pushd</EM>.</P></BLOCKQUOTE><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">los% <CODECLASS="userinput"><B>pushd ~/Mail</B></CODE>   ...<EMCLASS="emphasis">current directory becomes ~/Mail</EM>~/Mail ~/power </PRE></BLOCKQUOTE></P><PCLASS="para"><EMCLASS="emphasis">pushd</EM> prints the entire stack, giving me some confirmation aboutwhere I am, and where I can go.When I'm done reading the old mail,I want to move back:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">los% <CODECLASS="userinput"><B>popd</B></CODE>   ...<EMCLASS="emphasis">current directory becomes ~/power</EM>~/power </PRE></BLOCKQUOTE></P><PCLASS="para">We're back where we started; the <EMCLASS="emphasis">Mail</EM> directory is no longer onthe stack.</P><PCLASS="para">What if you want to move back and forth repeatedly?<EMCLASS="emphasis">pushd</EM>, withno arguments, just switches the two top directories on the stack.Like this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">los% <CODECLASS="userinput"><B>pwd</B></CODE>   ...<EMCLASS="emphasis">current directory is ~/power</EM>/home/los/mikel/powerlos% <CODECLASS="userinput"><B>pushd ~/Mail</B></CODE>   ...<EMCLASS="emphasis">current directory becomes ~/Mail</EM>~/Mail ~/power los% <CODECLASS="userinput"><B>pushd</B></CODE>   ...<EMCLASS="emphasis">current directory becomes ~/power</EM>~/power ~/Mail los% <CODECLASS="userinput"><B>pushd</B></CODE>   ...<EMCLASS="emphasis">current directory becomes ~/Mail</EM>~/Mail ~/power </PRE></BLOCKQUOTE></P><PCLASS="para">And so on.</P><PCLASS="para">If you like, you can let your directory stack get really long.Inthis case, two special commands are useful.<CODECLASS="literal">popd&amp;nbsp;</CODE>+<CODECLASS="replaceable"><I>n</I></CODE>deletes the <EMCLASS="emphasis">n</EM> entry in the stack.Entries are counted &quot;down&quot;from the top, starting with zero; that is, your current directory is 0.So <CODECLASS="literal">popd +0</CODE> and <CODECLASS="literal">popd</CODE> are the same.If <EMCLASS="emphasis">n</EM> is greaterthan 0, your current directory does not change.This may seemsurprising, but it isn't; after all, you haven't changed the top ofthe stack.</P><PCLASS="para">The command <CODECLASS="literal">pushd +</CODE><CODECLASS="replaceable"><I>n</I></CODE> &quot;rotates&quot; the stack, so that the<EMCLASS="emphasis">n</EM>th directory moves to the top, becoming the current directory.Note that this is a &quot;rotation&quot;: the whole stack moves.I don't findthe <CODECLASS="literal">+</CODE><CODECLASS="replaceable"><I>n</I></CODE> commands too useful, but you should know about them.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-15413"></A>The <EMCLASS="emphasis">dirs</EM> command prints the directory stack.It's a good way tofind out where you are.Some people like to <SPANCLASS="link">put the <EMCLASS="emphasis">dirs</EM> command in their prompt (<ACLASS="linkend"HREF="ch07_11.htm"TITLE="dirs in Your Prompt: Better than $cwd ">7.11</A>)</SPAN>,but I personally find incredibly long prompts more annoying than helpful.</P><PCLASS="para">The one drawback to <EMCLASS="emphasis">pushd</EM> and <EMCLASS="emphasis">popd</EM> is that you can easilybuild up a gigantic directory stack full of useless directories.Isuppose this doesn't really hurt anything, but it's needless clutter.The only way to clear the stack is to <EMCLASS="emphasis">popd</EM> repeatedly (except,in <EMCLASS="emphasis">tcsh</EM>, the command <EMCLASS="emphasis">dirs&amp;nbsp;-c</EM> clears the stack).More tothe point, the directories you're most likely to <EMCLASS="emphasis">want</EM> are at thetop of the stack.There's no really convenient way to save them.I mean, with 7 directories in the stack, you could conceivably dosomething like:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>pushd +5 ; popd ; popd</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">to get rid of the bottom two elements.The <EMCLASS="emphasis">pushd</EM> moves thebottom two elements of a 7-directory stack to the top.A bitinconvenient.[Clearing the whole stack is a good use for the C shell<SPANCLASS="link"><EMCLASS="emphasis">repeat</EM> (<ACLASS="linkend"HREF="ch09_25.htm"TITLE="The C Shell repeat Command ">9.25</A>)</SPAN>command.For example, if the stack has 7 directories, type:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>repeat 6 popd</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">That's an easy way to start over when the stack gets too messy. <EMCLASS="emphasis">&amp;ndash;JP</EM>&amp;nbsp;]</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-15438"></A><EMCLASS="emphasis">tcsh</EM> has a <EMCLASS="emphasis">savedirs</EM><SPANCLASS="link">shell variable  (<ACLASS="linkend"HREF="ch06_08.htm"TITLE="Shell Variables ">6.8</A>)</SPAN>.If you set <EMCLASS="emphasis">savedirs</EM>, <EMCLASS="emphasis">tcsh</EM> will save your directory stack to thefile <CODECLASS="replaceable"><I>~/.cshdirs</I></CODE> when you log out, and reset the same stack when youlog in again.</P><BLOCKQUOTECLASS="note"><PCLASS="para"><STRONG>NOTE:</STRONG> <ACLASS="indexterm"NAME="AUTOID-15448"></A><ACLASS="indexterm"NAME="AUTOID-15451"></A>The Korn shell has some similar (but not quite as general)features.  It keeps track of your previous working directory, and thendefines the special command <CODECLASS="literal">cd -</CODE> as &quot;change to the previousworking directory.&quot;</P></BLOCKQUOTE><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">ML</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_05.htm"TITLE="14.5 Saving Time When You Change Directories: cdpath "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 14.5 Saving Time When You Change Directories: cdpath "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_07.htm"TITLE="14.7 Nice Aliases for pushd "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 14.7 Nice Aliases for pushd "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">14.5 Saving Time When You Change Directories: cdpath </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.7 Nice Aliases for pushd </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 + -