Navigation

The pushd command

Pushes the current folder onto the stack and enters another. popd goes back.

Syntax

pushd <directorio>Try it in the terminal →

Most useful options

-nOnly changes the stack: does not change folder when adding or rotating entries.
+NRotates the stack so that entry N, counting from the left of dirs starting at 0, is on top.
-NSame as +N, but counting from the right.
--Marks the end of options, useful if the folder name starts with a dash.

Copy-ready examples

$ pushd Documentosenters Documentos and stacks the previous folder. pushd prints the stack.

Example output

~/Documentos ~
$ pushd Documentos && pushd ../Proyectosstacks a second folder: the stack now has three entries and the top is the current one.

Example output

~/Documentos ~
~/Proyectos ~/Documentos ~
$ pushd Documentos && pushd ../Proyectos && dirs -vnumbers the stack with dirs -v. Entry 0 is the folder you are in.

Example output

 0  ~/Proyectos
 1  ~/Documentos
 2  ~

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

Common errors

MessageCauseFix
bash: pushd: no other directoryYou called pushd with no arguments and an empty stack: there is no other folder to swap with.Pass a folder, for example pushd Documentos.
bash: pushd: /nonexistente: No such file or directoryThe folder does not exist or is misspelled.Check the path with ls before stacking it.
bash: pushd: -x: invalid numberpushd only accepts -n, a number with + or -, or --. Any other letter with a dash is rejected.Use pushd -n, or pass the folder after --.

Typical mistakes

  • pushd prints the stack every time it is used. In a script, redirect the output with > /dev/null if you do not want it.
  • If the folder does not exist, pushd gives an error and the change does not happen.
  • The stack lives in the shell: each terminal has its own and it is lost when you close it.

Differences from similar commands

  • popd: removes the top of the stack and takes you to the folder left on top.
  • cd: changes folder without stacking anything. The previous folder is not saved.
  • dirs: shows the stack without changing it. pushd does change it.

Related commands

FAQ

What does pushd do with no arguments?

It swaps the top two entries of the stack: you go back to the previous folder and the current one is saved.

Can I stack a folder without entering it?

Yes, with pushd -n. It adds the folder to the stack and your current directory does not change.

How do I stack a folder whose name starts with a dash?

With pushd -- name. The double dash ends the options and the name is treated as a folder.

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