As is well known, cd without an argument changes one's current directory to their home directory. I am wondering if there is any way, after using the cd command like this, to return to the directory location one was at prior to running cd?

Generalizing, is there any way of returning to a previous directory after one has cd-ed to another directory? It seems very possible that the answer to these two questions will be the same but in case it isn't I kept them both separate, because the first question is the main one I would like answered.

3 Answers 3



cd -


cd "$OLDPWD"

(You can omit the quotes if the directory name contains no whitespace or wildcard characters.)

These work in all POSIX-style shells (bash, ksh, zsh, …). cd - also works in tcsh and fish.

  • I tried with tcsh. cd - worked just fine, but cd $OLDPWD just takes me back to my home directory. Jun 3, 2014 at 20:03
  • @Joshua This means that tcsh doesn't set OLDPWD so it expanded cd $OLDPWD into cd.
    – Joseph R.
    Jun 3, 2014 at 20:04
  • but when I do echo $OLDPWD I get the path to my home directory as output. Jun 3, 2014 at 21:23
  • cd - and cd "$OLDPWD" work in all POSIX shells. Tcsh has cd - but not OLDPWD. Jun 3, 2014 at 23:40

I often times use pushd and popd to avoid this problem.


$ pwd

$ ls -l
total 12
drwxrwxr-x. 2 saml saml 4096 Jun  3 16:05 dir1
drwxrwxr-x. 2 saml saml 4096 Jun  3 16:05 dir2
drwxrwxr-x. 2 saml saml 4096 Jun  3 16:05 dir3

Now let's go to dir1.

$ pushd dir1
~/tst/134317/dir1 ~/tst/134317

$ pwd

And to dir2:

$ pushd ../dir2/
~/tst/134317/dir2 ~/tst/134317/dir1 ~/tst/134317

$ pwd

Now back to where ever we just came from:

$ popd
~/tst/134317/dir1 ~/tst/134317

$ pwd

You can see what directories are on your stack with the dirs command:

$ dirs
~/tst/134317/dir1 ~/tst/134317

The directory you're currently in is the directory furthest to the left.


The directory we'll "pop" to is the directory next to it on the right.


It continues to "push" directories on the left side as you add them, and "pops" them from the left side as well.


With bash, an alternative to "cd -" could be pushd/popd, it allows to work with a directory stack :

cd <path1> => move to <path1>
pushd <path2> => move to <path2>
pushd <path3> => move to <path3>
popd => return to <path2>
popd => return to <path1>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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