pushd, popd, and dirs Commands in Linux Explained
Navigating deeply nested directory structures in the Linux terminal
using standard cd commands can be slow and repetitive. The
dirs, pushd, and popd commands
solve this by allowing users to manage a directory stack—a Last-In,
First-Out (LIFO) history of previously visited locations. This article
explains the core functions of these three built-in shell commands,
demonstrates how they manipulate the stack, and provides clear examples
of how to incorporate them into daily command-line workflows for faster
directory navigation.
The Directory Stack Concept
The directory stack is an internal list maintained by your shell
(such as Bash or Zsh) that stores directory paths. The current working
directory is always located at the very top (index 0) of
this stack. Using stack-based navigation allows you to save your current
place, jump elsewhere to perform an action, and instantly return without
retyping long file paths.
The dirs
Command: Viewing the Stack
The dirs command displays the list of directories
currently held in the stack.
When run by itself without arguments, dirs outputs the
entire stack on a single line, separated by spaces:
dirsCommon Options for
dirs:
dirs -v: Displays the directory stack as a vertical numbered list. This is particularly useful for identifying the numerical index of each directory in the stack.dirs -c: Clears the entire directory stack, resetting it to only contain your current working directory.dirs -l: Prints the full, unabbreviated paths rather than substituting the home directory with a tilde (~).
The
pushd Command: Adding Directories to the Stack
The pushd command performs two tasks simultaneously: it
adds a specified directory to the top of the stack and immediately
navigates your terminal into that directory.
pushd /var/logWhen you execute this, /var/log becomes the active
directory, and your previous directory is placed just below it in the
stack. Upon execution, pushd automatically prints the
updated stack.
Key Behaviors of
pushd:
pushd(without arguments): Swaps the top two directories on the stack. This acts as an instant toggle between your current directory and the one you visited immediately prior.pushd +N: Rotates the stack so that the Nth directory (counted from the left or top, starting at 0) moves to the top, changing your working directory to it.pushd -N: Rotates the stack using the Nth directory counted from the right or bottom.
The
popd Command: Removing Directories from the Stack
The popd command removes a directory from the stack and
navigates to the directory that moves up to take its place.
popdRunning popd with no arguments discards the top
directory (your current working location) from the stack and immediately
changes your active directory to the new top entry.
Key Behaviors of popd:
popd +N: Removes the Nth directory from the stack (counted from the top/left) without changing your current directory, unless you pop index0.popd -N: Removes the Nth directory counted from the bottom/right of the stack.
Practical Workflow Example
Here is how the three commands operate together in a real session:
Start in your home directory:
pwd # Output: /home/userPush to an application directory:
pushd /etc/nginx # The stack now contains: /etc/nginx /home/userPush to a logs directory:
pushd /var/log # The stack now contains: /var/log /etc/nginx /home/userView the indexed stack:
dirs -v # Output: # 0 /var/log # 1 /etc/nginx # 2 /home/userReturn to the previous location:
popd # Stack is now: /etc/nginx /home/user # Current directory changes automatically to: /etc/nginxPop the final pushed directory to return home:
popd # Stack is now: /home/user # Current directory changes automatically to: /home/user
By utilizing pushd, popd, and
dirs, you can maintain a fluid, temporary history of
project paths and avoid typing lengthy absolute paths across complex
directory trees.