Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The command

cd -

will change the pwd to the last directory.

Is there a way to know which directory it is, so that I'll know where I am CD'ing to?

share|improve this question

migrated from Dec 19 '09 at 2:46

This question came from our site for professional and enthusiast programmers.

Yes, it is $OLDPWD.

~$ cd src/
~/src$ cd ..
~$ echo $OLDPWD
share|improve this answer
Thanks Dirk for the quick solution. – Abhijit Dec 18 '09 at 13:04
Always a pleasure :) – Dirk Eddelbuettel Dec 18 '09 at 13:18
"echo ~-" will output the same information – njd Feb 19 '10 at 15:23

The previous directory is saved in $OLDPWD

share|improve this answer

You can also use ~- (anywhere you can use tilde expansion) instead of $OLDPWD:

# Copy a file from the previous working directory.
cp ~-/file1 .

There is also ~+ for $PWD, which is useful for commands which require an absolute directory:

./configure --prefix ~+/root

Note that things like --prefix=~+/root won't work due to the rules of tilde expansion; use --prefix="$PWD/root" instead.

share|improve this answer

You must log in to answer this question.