Navigation

The popd command

Removes the top folder from the stack and changes to it.

Most useful options

-nOnly changes the stack: with no number, removes the entry right after the top and stays in the same folder.
+NRemoves entry N counting from the left of dirs, starting at 0. popd +0 removes the first one.
-NRemoves entry N counting from the right, starting at 0. popd -0 removes the last one.
--Marks the end of options, useful if a folder name starts with a dash.

Copy-ready examples

$ pushd Documentos && popdenters Documentos stacking the current folder, then returns with popd.

Example output

~/Documentos ~
~
$ pushd Documentos && pushd ../Proyectos && popd +1 && pwdremoves Documentos from the stack without moving, since it is not the top. pwd confirms you are still in Proyectos.
$ pushd Documentos && pushd ../Proyectos && popd -n && pwdremoves the entry right after the top, Documentos here, without changing folder: pwd still shows Proyectos.

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
bash: popd: directory stack emptyThere are no stacked folders: you have not used pushd in this session or you already removed them all.Stack with pushd first, or use cd if you only want to change folder.
bash: popd: +5: directory stack index out of rangeThe stack has fewer entries than the number you asked for.Check the entries with dirs -v and use a number within the range.

Typical mistakes

  • If the stack is empty, popd gives an error and does not change folder.
  • popd only changes folder when the top changes. Removing an entry that is not the top with +N or -N does not move you.
  • The stack lives in the shell: closing the terminal loses it, and popd then has nothing to remove.

Differences from similar commands

  • pushd: adds a folder to the stack and changes to it. popd does the opposite.
  • cd -: goes back to the previous folder without using the stack. popd removes the entries that pushd saved.
  • dirs: shows the stack without changing it. It is how you see what popd will remove.

Related commands

FAQ

What does popd do with no arguments?

It removes the top folder from the stack and takes you to the one left on top.

Can I remove a folder without changing directory?

Yes, with popd -n. It removes the entry right after the top, not the top itself, and your current folder stays as it is.

How do I know which number to use with popd +N?

With dirs -v, which numbers the entries from 0 starting at the top. That same numbering is what +N uses.

More Navigation commands

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course