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:

dirs

Common Options for dirs:


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/log

When 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:


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.

popd

Running 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:


Practical Workflow Example

Here is how the three commands operate together in a real session:

  1. Start in your home directory:

    pwd
    # Output: /home/user
  2. Push to an application directory:

    pushd /etc/nginx
    # The stack now contains: /etc/nginx /home/user
  3. Push to a logs directory:

    pushd /var/log
    # The stack now contains: /var/log /etc/nginx /home/user
  4. View the indexed stack:

    dirs -v
    # Output:
    # 0  /var/log
    # 1  /etc/nginx
    # 2  /home/user
  5. Return to the previous location:

    popd
    # Stack is now: /etc/nginx /home/user
    # Current directory changes automatically to: /etc/nginx
  6. Pop 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.