Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

up vote 6 down vote accepted

Yes:

cd -

or

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.

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

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

Example

$ pwd
/home/saml/tst/134317

$ 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
/home/saml/tst/134317/dir1

And to dir2:

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

$ pwd
/home/saml/tst/134317/dir2

Now back to where ever we just came from:

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

$ pwd
/home/saml/tst/134317/dir1

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.

~/tst/134317/dir1

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

~/tst/134317

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

share|improve this answer

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>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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