re: #OneDevMinute: Go back to the Previous Directory VIEW POST


I use autopushd and popd and cd -

alias po=popd

cd - goes to the last directory. If you use it repeatedly, it bounced back and forth between two directories A <-> B

autopushd (which is the automatic version of pushd) put the directories that you've gone through on a stack. So say you do:

cd A
cd B
cd C

Your directory stack will be C, B, A.

popd # now we're in B
popd # now we're in A

They do similar, but different things and depending on what you're doing one will be useful where the other isn't.

I find that while working through source code autopushd and popd are what I prefer as I'm crawling through code.

cd - is very useful when you are looking in say the code path, and want to hope between the logs directory.


Cool beans! 👌

I forget which one but a Linux flavor didn't play well with popd maybe it was Windows Subsystem Linux. Anywho, popd is a different thing then what this tip is about. Did you know you can also use cd .. to accomplish what you just wrote as compared to popd?


I think popd pops off of the directory history stack, rather than the directory hierarchy itself; so if you do this:

$ pushd /tmp
$ pushd
$ pushd projects/

followed by popd; popd, you'll end up in /tmp/ once again. I'm guessing that autopushd aliases cd to pushd to make this more automatic; in zsh cd does that by default.

Makes a lot of sense. And I think typing


to go to prev dir is quite easy for me to remember. Wouldn't you agree? 🤔

correct, popd uses the pushd history; it does not use the directory hierarchy. Use cd .. for that.


popd is different. I'm sorry if it wasn't clear that I meant "this is a similar, but different tool". I did not mean for it to be "that's bad do this instead."

cd .. goes up a directory in the hierarchy. popd goes back in the pushd history.

If you're going "down" in folders, then yes they would happen to do the same thing. But if you're moving across folders, they'll do different things.

Here's a comparison (pretend autopushd is on):

cd /usr/bin
cd /tmp/logs
cd .. # will take you to /tmp
cd .. # will take you to /
cd /usr/bin
cd /tmp/logs
popd # will take you to /usr/bin
popd # will take you to where you were before the first command

Yes, that makes a lot of sense. Thanks for sharing! 👌

code of conduct - report abuse