Take the 2-minute tour ×


That is, if I'm in:


and then type:

cd /var/lib/fubarred_app/blargh/logs




share|improve this question
in addition to @bryan's excellent answer, you might consider creating a pair of aliases for each path so you can just type something like: "bazmoo" or "blarghlog" at the prompt :) –  warren Aug 17 '11 at 20:30
Why don't you just use GNU screen? –  Shi Aug 17 '11 at 20:43
Which shell? that makes a huge difference. –  Randal Schwartz Aug 18 '11 at 2:22
@warren ditto. that's my preferred way to do everything. half of what i use my computer for has its own alias now... =) –  ixtmixilix Aug 19 '11 at 15:58
Fasd is awesome. Check it out. –  majkinetor Nov 5 '13 at 8:58
add comment

12 Answers

up vote 246 down vote accepted

The command

cd -


cd "$OLDPWD"


The POSIX man page for cd mentions:





cd "$OLDPWD" && pwd 


share|improve this answer
Thanks, this is exactly what I was looking for! –  Yitzchak Aug 17 '11 at 20:43
Keep in mind this really depends on the shell you're using, and since you didn't specify, it'd be hard to give the non-existent "portable" answer. –  Randal Schwartz Aug 18 '11 at 2:22
@Randall, Perhaps you could combine all your individual shell comments into one Voltron-Shell answer. –  hyperslug Aug 18 '11 at 2:46
cd - goes to the directory in the shell variable $OLDPWD –  Foo Bah Aug 18 '11 at 7:04
@sinni Certain flavors do have pushd, and at least it's a builtin in bash. –  slhck Aug 24 '11 at 7:00
show 6 more comments



charon:~ werner$ pushd Documents/
~/Documents ~

charon:Documents werner$ pushd ../Movies/
~/Movies ~/Documents ~

charon:Movies werner$ pushd ../Downloads/
~/Downloads ~/Movies ~/Documents ~


charon:Downloads werner$ popd
~/Movies ~/Documents ~

charon:Movies werner$ popd
~/Documents ~

charon:Documents werner$ popd

charon:~ werner$ popd
-bash: popd: directory stack empty


share|improve this answer
pushd and popd are not available in all shells. –  Randal Schwartz Aug 18 '11 at 2:23
Zsh has an AUTO_PUSHD option, which makes it push every directory you cd in on the stack, which I find very useful. –  Wiesław Herr Aug 18 '11 at 6:42
@Wiesław: Really? I wish bash had that... Of course, I could alias cd to pushd, and it'd mostly be the same. –  Chris Charabaruk Aug 18 '11 at 12:12
Or alias pd to pushd and have both. BTW pushd (or pd, if you alias it) by itself swaps the current directory and last-pushed directory, essentially accomplishing what 'cd -' does. –  JRobert Aug 18 '11 at 16:06
I like the concept but never got used to them. –  Thorbjørn Ravn Andersen Aug 18 '11 at 18:39
show 1 more comment

There are some "jump" programs

  • autojump (maintained with basic features)
  • j2 (apparently unmaintained with some advanced features)


In your case

~$ j baz 

~$ j bla 


j2 and z support multiple search terms,...

~$ j baz src

... and more options.

share|improve this answer
"j" is clearly shell specific. You didn't say that. –  Randal Schwartz Aug 18 '11 at 2:23
@Randal: How is it shell-specific? It's a program. –  Fixee Aug 21 '11 at 15:42
I'm an idiot. :) I see it now. –  Randal Schwartz Aug 24 '11 at 0:23
add comment

In addition to cd - and cd $OLDPWD



share|improve this answer
CTRL-R won't necessarily help. You might have been in /some/obscure/directory/deep and done "cd evendeeperdir" then gone to ~/somehere/different. –  justintime Aug 18 '11 at 14:24
Great! It works for other things than cd, great help! –  Niloct Aug 19 '11 at 15:57
add comment

You can use this to easily make aliases for directories:


a 1

and later:

share|improve this answer
Only on a shell that understands aliases and functions. Not all do. –  Randal Schwartz Aug 18 '11 at 2:23
add comment



bookmark foo


go foo

To see a list of the bookmarks:



go sho[tab]
share|improve this answer
add comment


share|improve this answer
add comment



Blog post for my directory management utility


share|improve this answer
add comment



j moo
share|improve this answer
add comment



from my .bashrc-file:

export CDPATH='.:~/source/'


cd myproject



share|improve this answer
add comment



Of course the first choice for jumping back and forth is

cd -


beam () 
    if [[ -d $ldir ]]; then
        echo $ldir;
        cd $ldir;
        echo "no directory "$ldir;


  • locate
  • grep
  • head


  • newer directories than the last updatedb-run aren't found





Advantage of my solution:

  • not limited to 2 directories, like cd -
share|improve this answer
add comment


cd -



    alias moo /etc/foo/bar/baz/moo  

    cd ~

This would allow changing a directory as easy as:

   cd ~/moo
   cd ~/blargh
share|improve this answer
add comment

protected by studiohack Aug 20 '11 at 3:31


Would you like to answer one of these unanswered questions instead?
