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.
Written by Paolo Di Tommaso
Related protips
9 Responses
cool -- didn't know about this one. Similar to pushd/popd.
 
i like it - very handy! +1
 
Do you know how many times I've wished there was something like this in existence? Thank you!
 
I knew there had to be a way to do this! Thanks.
 
if using zsh, it's even faster. all you need is the -
 
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"
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.
 
@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. 
 
See also git checkout -
 
 
 
 
