aq5sya
Last Updated: January 23, 2019
·
2.949K
· paolodt

Move back to the last visited directory

When you are typing commands in the shell you can move back to the last visited directory with the following trick:

cd -

Quick and useful.

10 Responses
Add your response

373

cool -- didn't know about this one. Similar to pushd/popd.

over 1 year ago ·
378

Comes in handy. +1

over 1 year ago ·
396

i like it - very handy! +1

over 1 year ago ·
417

Do you know how many times I've wished there was something like this in existence? Thank you!

over 1 year ago ·
459

I knew there had to be a way to do this! Thanks.

over 1 year ago ·
537

if using zsh, it's even faster. all you need is the -

over 1 year ago ·
1699

Pushd is good if you're thinking ahead, but not everyone does that ;)
I suppose you could alias cd to pushd since they do pretty much the same thing.

@plukevdh, you can do that in bash with alias -- -="cd - >/dev/null"

over 1 year ago ·
2223

On ZSH you can list the last n directories by typing 'd' and switching to one of them by typing the correspondent number. Numbers 1-9 are aliased to relative cd: "cd -", "cd +2", "cd +3", etc. For example:

$ d
0 ~
1 /etc
2 /Users
3 /var/log
4 /tmp

It means I'm currently at ~ and if I type '2' I'll return to the Users directory.

over 1 year ago ·
2445

@lccro, that's an oh-my-zsh alias (alias d='dirs -v'). You also want setopt auto_pushd to make cd push the old directory onto the directory stack.

I made a function tonight so you can use d to output the dirstack list and d 0 to go to the 0 match.

over 1 year ago ·
6444

See also git checkout -

over 1 year ago ·