I'm working through The Command Line Crash Course. I've been doing really well, but now find myself completely stumped at popd and pushd. I've read the analogy of a stack of plates or pancakes. I still don't get it, because I cannot visualize the code that comes after. For example:

pats-MacBook-Pro:temp pat$ pushd i/like/icecream

~/temp/i/like/icecream ~/temp

If pushd is like cd, then why does the second line show ~/temp at the end of the line? How does that part fit into the 'stack of plates' analogy?

Then if I type popd, it takes me back to the original folder, ~/temp. Why?

I really appreciate your help! Thanks!

  • I get it!!! I finally flipping get it! Thank you Jasper and garyjohn! dirs -v really, really helps! Thanks again, so much! I was starting to lose my marbles! – user315602 Apr 15 '14 at 12:51

pushd can take one argument (a directory name), stores the current directory on the DIRSTACK and changes the current working directory to the given directory. The manpage further explains:

If the pushd command is successful, a dirs is performed as well.

And dirs prints the currently remembered directories. popd's purpose is to pop (remove from stack) the "uppermost" remembered directory and cd into the new "uppermost" remembered directory. After a successful popd, again a dirs command is executed to print the current stack. If you do dirs in a fresh shell, you will see that the current workind directory is always contained in the DIRSTACK variable -- this is where your ~/temp comes from.

An example: assume we have directories one, one/two and three in our home directory:

~ $ pushd one   # change into one, remember current location
~/one ~
~/one $ pushd two   # change into two, remember current location
~/one/two ~/one ~   
~/one/two $ cd ../../three  # change into three, don't remember where we came from
~/three $ dirs  # show where we are and where we can get back to
~/three ~/one ~
~/three $ popd  # go back one step in the remembered directories
~/one ~
~/one $

You can think of the pushd/popd commands as a way to do "change into this directory, but remember where we came from (pushd); do things in the new directory; change directory back (popd). This will also get clearer if you do multiple pushds in a row without popding. Then the "stack of plates" analogy should become obvious.

| improve this answer | |

In order to conserve vertical space on the screen, pushd is showing you the stack horizontally, with new items being pushed from the left.

You started with one item on the stack, the working directory, ~/temp. When you executed pushd i/like/icecream, pushd pushed i/like/icecream onto the top of the stack, pushing ~/temp down. The way that pushd displayed the result, though, was to show that i/like/icecream had been pushed onto the stack from the left, moving ~/temp to the right. pushd also changes the working directory to the directory just pushed onto the stack, which was i/like/icecream.

popd pops the stack, removing the top item (i/like/icecream) and letting the next item (~/temp) pop to the top of the stack, becoming the new working directory.

popd, like pushd, shows the stack on its side, with the top of the stack on the left and the bottom of the stack on the right.

It might help you to visualize the current state of the stack with the dirs -v command, which displays the stack vertically.

| improve this answer | |

I found popd difficult at the beginning, too, but the following helped me:

for bash, basically: instead of using cd you can use pushd to change directorys, so they are stacked or to say it in other words: the history of changed directorys is saved.

pushd /home; pushd /var; pushd log

To see the stack (or history) use dirs and for easier navigation (to get the numbers of the "stack-entrys" use:

dirs -v


me@myhost:/home$ dirs -v
 0  /home
 1  /var
 2  /tmp

Now utilize these numbers with cd and ~ like:

cd ~1

... so you can navigate the "history" of directorys (meaning navigation in the stack)

But now these numbers are rearranged now and position "0" will change, so just pushd the directory to the top position twice (or use a dummy on position 0) like:

me@myhost:/home$ dirs -v
 0  /home
 1  /home
 2  /var
 3  /tmp

now 1..3 will keep there position I read this somewhere but do not know anymore, so sorry for not giving credit

(to release the current directory from the stack/deleting it from history use popd)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy