Go back to previous directory in shell

308

88

Is there a way to go back to previous directory we were in using bash,tcsh without using pushd/popd ? I'd like to type something like "back" and got returned to the previous directory I was in.

Edit:

"cd -" works, but only for current and previous directories. Is there anyway I can go back to the previous previous previous directory like how you can go back in the web browser?

Regards

Don Ch

Posted 2010-02-25T06:44:42.983

Reputation: 4 869

1As noted below, you can do so using "pushd" and "popd". – blueyed – 2010-03-05T03:02:53.097

1

Best answer imho : http://unix.stackexchange.com/a/180640/158177 provides cd -1 to cd -9 which I think is what the OP asked for

– Titou – 2016-09-29T15:11:28.580

8Just a side note "cd --" goes to the user default direcotry (/home/username) – sdaffa23fdsf – 2012-04-08T22:49:49.593

Answers

417

cd - (goes back to previous directory)

If you want to be able to go to the other previous directories, this is not possible out of the box. But check this script and instructions:

History of visited directories in BASH

The cd command works as usual. The new feature is the history of the last 10 directories and the cd command expanded to display and access it. cd -- (or simply pressing ctrl+w) shows the history. In front of every directory name you see a number. cd -num with the number you want jumps to the corresponding directory from the history.

Snark

Posted 2010-02-25T06:44:42.983

Reputation: 30 147

21also pushd and popd might be useful – lorenzog – 2010-02-25T08:55:37.687

6@lorenzog : lydonchandra, in his question, said "without using pushd/popd" – Snark – 2010-02-25T09:19:59.400

@ogc-nick for using this cd -- in menu-like manner, you should use the mentioned script – Ram – 2016-02-11T06:13:38.817

1

@ogc-nick no it doesn't. The -- simply separates a command and its options from the parameters (see this post). Because no arguments follow after --, the final command is just cd which switches to your home directory. That might have been the second previous directory, but that's just a coincidence.

– Griddo – 2018-03-08T09:02:12.127

wow syntactic sugar much – oldboy – 2018-06-24T02:00:14.357

you can type d to get list of latest directories. and cd -n to visit one where n is the index shown in d's output – dipu – 2018-07-21T10:16:30.723

27

You can also use variable cd $OLDPWD. This can be also used in shell scripts.

Ales Dolecek

Posted 2010-02-25T06:44:42.983

Reputation: 371

6$OLDPWD maintains the last directory you came from which is good for scripts. I use $OLDPWD with cp command a lot. E.g cp -v $OLDPWD/file . – Neil Wightman – 2015-01-09T09:12:43.113

1

For Windows (including Node.js commads prompt console case) does not works cd - To moves you up one directory works

cd ..

IgorBeaz

Posted 2010-02-25T06:44:42.983

Reputation: 205

3cd .. moves to parent directory, which is not what was the subject of question. – ViaSat – 2018-03-28T16:24:13.817

1This is bash here, not Windows. – Timo – 2018-04-05T12:37:17.340

0

I find the easiest way to do it is with this .bashrc power edit: https://github.com/wting/autojump . You get to "mark" folders you navigate to, giving them a shorthand name that's easy to remember (my advice; the foregoing is not in the docs), such as Pics for Pictures, etc. 'jump' returns you to the folder you 'marked,' and 'marks' lists folders you have added to the 'stack' (as with pushd and popd), with the added advantage that your marks remain the same from one session to the next, ad infinitum.

I have yet to try it on more than one harddrive, but the results should be similar to those using a single volume.

S Wright

Steve Wright

Posted 2010-02-25T06:44:42.983

Reputation: 11

-1

If you've gone down the directory tree and want to go back up, cd .. is my personal favorite. You can jump around within a branch of the tree quite easily with cd .. going up one directory and cd ../.. two and cd ../../.. three, etc. You can also go up and down a different branch with the same command, such as cd ../../example or cd ../../../example/example etc. For a simple switch that goes back and forth between directories, cd - or $OLDPWD are your best bets, as others mentioned.

Adam Erickson

Posted 2010-02-25T06:44:42.983

Reputation: 147

Any particular reason why this was downvoted? It contains correct answers, though perhaps not the only ones. – Adam Erickson – 2019-04-18T09:52:17.940

-2

I think cd .. might help. If you do a ls -a in any directory you would see that there are two entries: one named "." and another named ".."; the single dot is reference to the directory you are already in, while the double is the previous directory in the path.

Xente

Posted 2010-02-25T06:44:42.983

Reputation: 83

4This answer provides useful information even if it does not answer correctly the question. Therefore there is no point in piling negative votes on it, I upvoted for the effort. – Titou – 2016-09-29T12:36:55.353

1This is not Berkley, there are no special awards just for participation – nathanchere – 2017-05-24T09:45:10.277

23.. is not the previous directory, it's just the parent directory. – Der Hochstapler – 2012-12-10T10:28:52.443