change to the previous working directory

cd -

By: systemj
2009-02-04 22:41:21

4 Alternatives + Submit Alt

What Others Think

cd ./- if you want to change to a dir named '-'
eneville · 610 weeks and 4 days ago
if you like this also look into pushd/popd. it's in the bash man page i think.
alexfoo · 609 weeks and 2 days ago
pushd/popd are cool but a bit obscure ... "cd -" is straightforward !
CodSpirit · 607 weeks and 4 days ago
always used pushd/popd for this till now.. but this is awesome =)
unixmonkey2498 · 605 weeks and 6 days ago
comes in handy :)
chanux · 605 weeks and 5 days ago
Wow! Using Unix/Linux for 18 years, and only TODAY found this bit of info about so very common command - cd ! Amazing!
vleolml · 598 weeks and 4 days ago
why not just "cd .." ???
wrongloop · 598 weeks and 2 days ago
I've got to remember that one. "cd .." doesn't work for that if the last cd command was "cd ~/foo" rather than "cd bar".
codetinker · 598 weeks ago
"cd .." goes to the previous directory in the tree, "cd -" goes to the previous _working_ directory.
sedcommand · 589 weeks and 6 days ago
how about "cd $OLDPWD" ? need to just type in "cd $OL" and tab. bash will take care of the rest :)
ninadsp · 583 weeks and 4 days ago
Oh my god, I have been flubbing with pushd/popd (only work if you know ahead of time) for .. decades. I didn't even know this existed. Damn.
cj_ · 570 weeks and 6 days ago
Seems ? cd -- ? jumps to the ~/ directory?
hced · 480 weeks and 1 day ago
I was always wondering if something like a "cd history" like this existed. Thanks. But it only remembers the *last* working dir. Is there no way to use it as a stack? Like with pushd/popd? But more automatic? I was already thinking about aliasing "cd" to "pushd . && cd", but that seems a bit wonky...
exhuma · 445 weeks and 6 days ago
The zsh config "setopt AUTO_PUSHD" automatically pushes to dir stack, and I've aliased "p=popd" so at any point I can type "p" to navigate up my working directory history.
cec · 407 weeks and 4 days ago
Why is this marked as malicious?
benjabean1 · 267 weeks ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: