This question already has an answer here:

I often use cd - to go back to where I was. How can I do this multiple times in bash? Or would zsh or some other tool support this?

marked as duplicate by Hauke Laging, Anthon, Networker, John WH Smith, jimmij Jan 15 '15 at 8:47

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Check this out superuser.com/a/449705/235569 – Braiam Jul 25 '13 at 19:50
  • 1
    @Braiam: The poster is looking to go to a previously-visited directory, not a directory above their current directory. – user26112 Jul 25 '13 at 19:54
  • Since I can't edit, I've used pushd but find it basically unusable since it requires you to do all of this work ahead of time (basically you have to manage it). I'm REALLY surprised what I want to do is not supported; it is 2013 – timpone Jul 25 '13 at 20:00
  • Check your notifications, there is a edit on queue, you can cancel that and edit your question. – Braiam Jul 25 '13 at 20:06

In zsh, there's an auto_pushd option. This option makes cd behave like pushd. Then you can just use popd to go back to previous directories.

~ $ setopt auto_pushd
~ $ cd /
/ $ cd /var
/var $ cd /usr
/usr $ dirs
/usr /var / ~
/usr $ popd
/var $ popd
/ $ popd
~ $

In Bash, you can alias cd to pushd.

alias cd=pushd

The one downside of this is that you will lose cd's three flags. From the cd help entry:

-L force symbolic links to be followed
-P use the physical directory structure without following symbolic links
-e if the -P option is supplied, and the current working directory cannot be determined successfully, exit with a non-zero status

If you ever have to use the actual cd builtin instead of the alias, you can use one of these:

  • 'cd' - Quoting the command makes the shell not resolve the alias and use the normal cd.
  • \cd - Backslashes quote characters. If you quote one character of a word, the shell treats the whole word as quoted.
  • builtin cd - This directly tells the shell to use the builtin instead of the alias.
  • cool, is there a way to do this in bash or just zsh? – timpone Jul 25 '13 at 20:04
  • @timpone: See the edit to my answer. – user26112 Jul 25 '13 at 20:11
  • 1
    thx for pointing out tradeoffs. – timpone Jul 25 '13 at 20:21
  • Regarding the bash solution: there are more tradeoffs to be considered. E.g., I would too often write cd, meaning cd ~, and getting the swap of the two dirs at the top of the stack. I prefer simply using pushd when I need it. But I always take one alias with me: alias ad=pushd (ad stands for alternate directory) – Walter Tross Aug 4 '13 at 14:00

Here's a quick and dirty way to conveniently bookmark directories and jump back to them:

$ a() { alias $1="cd $PWD"; }

Go somewhere and type:

$ a 1

You now have a command called 1 which does cd the directory that is current at this time. Later just type:

$ 1

And presto, back to that directory. I find this very useful.

  • 7
    +1. This is an excellent idea. It will break if your current working directory has a space in it somewhere though. To fix that, you should put $PWD in double quotation marks: "$PWD". – user26112 Jul 26 '13 at 1:39
  • Now all you need is to make it keep a list of the bookmarks, so you can have a command to remove them (so that you don't accidentally delete non-bookmark aliases) or clear all of them. – AJMansfield Jul 26 '13 at 16:37


I found a script that solved this issue for me. With this you can type cd -- to see the last 10 directories that you've used. It'll look something like this:

0  ~/Documents/onedir
1  ~/Music/anotherdir
2  ~/Music/thirddir
3  ~/etc/etc

To go to ~/Music/thirddir just type cd -2



oh-my-zsh provides some really nice functionality for this (at least I think it's oh-my-zsh that sets this).

Basically, d is aliased to dirs -v | head -10

dirs is a zsh built-in command and shows the last directories you've been to.

Also, 1 is aliased to cd -1 and so on for all numbers to 9.

In practice, it works like this:

$ pwd

$ d
0       ~/Documents/gems/java_regex/lib
1       ~/Documents/gems/java_regex
2       ~/Documents/gems
3       ~/Documents
4       ~

$ 2

$ pwd

You can use the pushd builtin in bash to push new working directories onto the directory stack. Use popd to pop directories of the stack in order to return to the previous working directories.

  • thx, should have mentioned that I tried pushd in past but was really cumbersome. Just surprised this doesn't exist. – timpone Jul 25 '13 at 20:01
  • @timpone: Maybe you could try aliasing cd to pushd. Some suggestions on this are discussed in this question. – Thomas Nyman Jul 25 '13 at 20:09
  • thx, upvoted for response – timpone Jul 25 '13 at 20:19

I'm using simple function found on commandlinefu long time ago

 up() { local x='';for i in $(seq ${1:-1});do x="$x../"; done;cd $x; }

(added in my osx .profile works like a charm)


Yes. We can make your cd command more powerful. Bash profile allows us to customize it the way we want.

Rather than doing multiple cd to navigate up the directory, we can use alias

For example :

alias ..5="cd ../../../../.."

This will take you 5 directory up the level.

  • Question is "How can I hit the back button" on where I've been (Previous locations) - not "How can I go up a directory (or 5)". – WernerCD Jul 26 '13 at 17:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.