aq5sya
Last Updated: February 25, 2016
·
2.909K
· 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.

Say Thanks
Respond

10 Responses
Add your response

373
Thumb alyssa pohahau

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

over 1 year ago ·
378
81609937c20aeab3598aea56f1f4f022

Comes in handy. +1

over 1 year ago ·
396
3a4e18a3d3caa5da26b9cfed37852f25

i like it - very handy! +1

over 1 year ago ·
417
Cd0ac1afd622386fcc12926a23f5848c

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

over 1 year ago ·
459
110742409f62f4e03e074ff1b5aa9a4c

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

over 1 year ago ·
537
0945810eafa4ac5387084a7815488579

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

over 1 year ago ·
1699
Ea20769072a6038d72fba0cc2806b479

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
Visual2015

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
A1f621f6e2cb4857b185c1e00efd6fa8

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

See also git checkout -

over 1 year ago ·