Last Updated: September 09, 2019
·
3.275K
· 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.

9 Responses
Add your response

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

over 1 year ago ·

i like it - very handy! +1

over 1 year ago ·

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

over 1 year ago ·

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

over 1 year ago ·

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

over 1 year ago ·

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 ·

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 ·

@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 ·

See also git checkout -

over 1 year ago ·