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.
pushd [-n] [DIR | +N | -N] popd [-n] [+N | -N] dirs [-clpv] [+N | -N]
Try it
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.
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
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 -vnumbers it 0;pushd DIRadds a new entry 0 and moves the old one to 1. - They print the stack after every successful
pushdandpopd. In scripts, silence it with> /dev/null. - Always pair them. A function that
pushds and returns early on error leaves you in another directory. Usepopdon 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. Usepushdwhen you want an interactive stack or need to come back to several directories. cd -versuspushd:cd -toggles between two directories; the stack remembers as many as you like.- Not portable: these builtins do not exist in
dashor POSIXsh. A script with#!/bin/shthat callspushdfails on Debian-style systems. dirsshows~for your home directory unless you use-l.- Tab completion and
~Nexpansion work with the stack in Bash:cd ~2jumps to entry 2,ls ~1lists it.