<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Joy of Hack &#187; code</title>
	<atom:link href="http://www.aijazansari.com/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.aijazansari.com</link>
	<description>For people who like to make things</description>
	<lastBuildDate>Tue, 20 Jul 2010 13:20:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Navigating the Directory Stack in &#8216;bash&#8217;</title>
		<link>http://www.aijazansari.com/2010/02/20/navigating-the-directory-stack-in-bash/</link>
		<comments>http://www.aijazansari.com/2010/02/20/navigating-the-directory-stack-in-bash/#comments</comments>
		<pubDate>Sat, 20 Feb 2010 23:40:22 +0000</pubDate>
		<dc:creator>Aijaz Ansari</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[My Software]]></category>
		<category><![CDATA[Productivity]]></category>
		<category><![CDATA[Shell]]></category>

		<guid isPermaLink="false">http://www.aijazansari.com/?p=338</guid>
		<description><![CDATA[If you&#8217;re like me, you spend a lot of time jumping from project to project in a Linux shell.  I find that I have to switch back and forth between directories.  The bash shell has commands to maintain a stack of directories.  I&#8217;ve written some functions that use these utilities to make directory navigation easier. [...]]]></description>
			<content:encoded><![CDATA[<p><a rel="attachment wp-att-431" href="http://www.aijazansari.com/2010/02/20/navigating-the-directory-stack-in-bash/img_4950-edit/"><img class="alignleft size-medium wp-image-431" title="Barracks by the Taj Mahal" src="http://www.aijazansari.com/wp-content/uploads/2010/02/IMG_4950-Edit-285x190.jpg" alt="Barracks by the Taj Mahal" width="285" height="190" /></a>If you&#8217;re like me, you spend a lot of time jumping from project to project in a Linux shell.  I find that I have to switch back and forth between directories.  The <em>bash</em> shell has commands to maintain a <a title="What is a stack?" href="http://www.ece.cmu.edu/~koopman/stack_computers/sec1_2.html">stack</a> of directories.  I&#8217;ve written some functions that use these utilities to make directory navigation easier. I&#8217;ve found these functions very useful, and perhaps you will too.  Let&#8217;s see them in action first with some examples, and then look at the code:</p>
<div class="aaa_code_section">
<p>In this first snippet, I start working in the Documents/Training/qt/ch1 directory:</p>
<pre class="brush: bash;">
$ pwd
/Users/aijaz
$ cd Documents/Training/qt/ch1/
$ ls
p1    t2
$
</pre>
</div>
<div class="aaa_code_section">
<p>Now let&#8217;s say I have to work in the TaskForest lib directory for a while:</p>
<pre class="brush: bash;">
$ cd ~/Projects/projects/taskforest/lib/TaskForest/
$ ls -l
total 352
-rw-r--r--   1 aijaz  aijaz   8816 May 25  2009 Calendar.pm
...
-rw-r--r--   1 aijaz  aijaz   4558 May 25  2009 TimeDependency.pm
$
</pre>
</div>
<div class="aaa_code_section">
<p>Now my work gets preempted because I have to work in the &#8216;rates&#8217; directory:</p>
<pre class="brush: bash;">
$ cd ~/tc/config/rates/
$ ls -l
total 27544
drwxr-xr-x  21 aijaz  aijaz       714 Sep  3  2007 Text
...
-rwxr-xr-x   1 aijaz  aijaz       569 Sep  3  2007 findVoipAccess.pl
$
</pre>
</div>
<div class="aaa_code_section">
<p>After finishing my work in the rates directory, I want to get back to what I was doing, but I can&#8217;t remember exactly what where I was before I got interrupted.  So I enter the &#8216;d&#8217; command which displays the stack of directories.  Every time I used the &#8216;cd&#8217; command, the system pushed the directory I was in onto a stack.  The &#8216;d&#8217; command displays the stack and prompts me for an entry. If I enter a number, it pushes the directory that&#8217;s at that position in the stack to the top, and enters that directory.</p>
<pre class="brush: bash;">
$ d
 0  ~/tc/config/rates
 1  ~/Projects/projects/taskforest/lib/TaskForest
 2  ~/Documents/Training/qt/ch1
 3  ~
#: 2
$ pwd
/Users/aijaz/Documents/Training/qt/ch1
$ d
 0  ~/Documents/Training/qt/ch1
 1  ~/tc/config/rates
 2  ~/Projects/projects/taskforest/lib/TaskForest
 3  ~
#: q
$
</pre>
<p>You can see that when I entered &#8217;2&#8242; above, the &#8216;d&#8217; command pushed the &#8216;~/Documents/Training/qt/ch1&#8242; directory to the top of the stack and entered that directory.  You can see the modified directory stack above.  I entered &#8216;d&#8217; again to view the directory stack, but this time entered &#8216;q&#8217; to do nothing.</p>
</div>
<div class="aaa_code_section">
<p>I&#8217;ve also created the &#8216;p&#8217; command, which pops the current directory off the top of the stack and enters the directory that was under it.</p>
<pre class="brush: bash;">
$ p
$ pwd
/Users/aijaz/tc/config/rates
$ d
 0  ~/tc/config/rates
 1  ~/Projects/projects/taskforest/lib/TaskForest
 2  ~
#: q
$
</pre>
</div>
<p>Now let&#8217;s have a look at the code that makes this work.  You can copy and paste this code directly into your <em>.bashrc</em> file.</p>
<pre class="brush: bash;">
# An enhanced 'cd' - push directories
# onto a stack as you navigate to it.
#
# The current directory is at the top
# of the stack.
#
function stack_cd {
    if [ $1 ]; then
        # use the pushd bash command to push the directory
        # to the top of the stack, and enter that directory
        pushd &quot;$1&quot; &gt; /dev/null
    else
        # the normal cd behavior is to enter $HOME if no
        # arguments are specified
        pushd $HOME &gt; /dev/null
    fi
}
# the cd command is now an alias to the stack_cd function
#
alias cd=stack_cd

# Swap the top two directories on the stack
#
function swap {
    pushd &gt; /dev/null
}
# s is an alias to the swap function
alias s=swap

# Pop the top (current) directory off the stack
# and move to the next directory
#
function pop_stack {
    popd &gt; /dev/null
}
alias p=pop_stack

# Display the stack of directories and prompt
# the user for an entry.
#
# If the user enters 'p', pop the stack.
# If the user enters a number, move that
# directory to the top of the stack
# If the user enters 'q', don't do anything.
#
function display_stack
{
    dirs -v
    echo -n &quot;#: &quot;
    read dir
    if [[ $dir = 'p' ]]; then
        pushd &gt; /dev/null
    elif [[ $dir != 'q' ]]; then
        d=$(dirs -l +$dir);
        popd +$dir &gt; /dev/null
        pushd &quot;$d&quot; &gt; /dev/null
    fi
}
alias d=display_stack
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.aijazansari.com/2010/02/20/navigating-the-directory-stack-in-bash/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
