The CD command with "-" argument will change to previous directory. Is there a way to know which directory it is?

5

1

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?

Abhijit

Posted 2009-12-18T12:46:11.570

Reputation:

Answers

15

Yes, it is $OLDPWD.

~$ cd src/
~/src$ cd ..
~$ echo $OLDPWD
/home/$USER/src

Dirk Eddelbuettel

Posted 2009-12-18T12:46:11.570

Reputation: 1 191

1"echo ~-" will output the same information – njd – 2010-02-19T15:23:15.940

Always a pleasure :) – None – 2009-12-18T13:18:54.157

5

The previous directory is saved in $OLDPWD

abyx

Posted 2009-12-18T12:46:11.570

Reputation: 465

2

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.

Mark Edgar

Posted 2009-12-18T12:46:11.570

Reputation: