Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Files and Directories

Bash pushd, popd and dirs

Reference for the Bash pushd, popd and dirs builtins: keep a stack of directories, jump between them, rotate with +N and use them safely in scripts.

pushd, popd and dirs manage a stack of directories. pushd DIR changes to DIR and remembers the directory you were in; popd goes back to the most recent one; dirs shows the stack. It is a more flexible version of cd -, which only remembers a single previous directory.

They are Bash (and Zsh, Ksh) builtins, not programs, so they affect the current shell. Plain POSIX sh has none of them. The stack is most useful in scripts that need to visit several directories and return reliably.

Terminal
pushd [-n] [DIR | +N | -N]
popd  [-n] [+N | -N]
dirs  [-clpv] [+N | -N]

Try it

Bash Push, pop and list

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Rotating the stack and safe use in a function

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
pushd [-n] [DIR]
pushd [-n] +N | -N
popd  [-n] [+N | -N]
dirs  [-clpv] [+N | -N]
Command Description
pushd DIR Push the current directory, then cd to DIR; the new directory is the top (entry 0).
pushd (no argument) Swap the top two entries.
pushd +N / pushd -N Rotate the stack so entry N (counted from the left / right of dirs output) comes to the top and becomes the working directory.
popd Remove the top entry and cd to the new top.
popd +N / popd -N Remove entry N without changing directory (unless it is the top).
dirs Print the stack on one line; entry 0 is the current directory.
dirs -p / dirs -v One entry per line / numbered, one per line.
dirs -l Print full paths instead of ~.
dirs -c Clear the stack.
-n (pushd, popd) Do not change directory; only manipulate the stack.

Returns: 0 on success; non-zero when the directory does not exist, or popd is run on an empty stack ("directory stack empty").

Usage notes

  • The stack's first entry is always the current directory. dirs -v numbers it 0; pushd DIR adds a new entry 0 and moves the old one to 1.
  • They print the stack after every successful pushd and popd. In scripts, silence it with > /dev/null.
  • Always pair them. A function that pushds and returns early on error leaves you in another directory. Use popd on every exit path, or work in a subshell ( cd dir && ... ), which needs no cleanup.
  • A subshell is often simpler: (cd "$dir" && make) cannot affect the caller. Use pushd when you want an interactive stack or need to come back to several directories.
  • cd - versus pushd: cd - toggles between two directories; the stack remembers as many as you like.
  • Not portable: these builtins do not exist in dash or POSIX sh. A script with #!/bin/sh that calls pushd fails on Debian-style systems.
  • dirs shows ~ for your home directory unless you use -l.
  • Tab completion and ~N expansion work with the stack in Bash: cd ~2 jumps to entry 2, ls ~1 lists it.