pushd - Linux


The pushd command is used in Linux to save the current directory that you are in and then changes to a new directory. It basically “pushes” the directory onto a stack and you can return to it later using the popd command. This is especially useful in scripts and shell sessions where operations involve moving between directories efficiently.


The basic syntax for using pushd command is as follows:

pushd [options] [directory]

Where [directory] is the path to the directory you want to switch to. If no directory is provided, pushd switches places with the top directory on the stack and the current directory.


  • +n: Rotates the stack so that the nth directory (counting from the left of the list shown by dirs, starting with zero) is at the top.
  • -n: Rotates the stack so that the nth directory (counting from the right of the list shown by dirs, starting with zero) is at the top.
  • -nolinks: Avoid resolving symbolic links by treating them as non-links. This is helpful for avoiding confusing paths when working with linked directories.


  1. Basic Usage:
    Switch to a directory and push the old directory to the stack:

    pushd /var/log
  2. Toggle Between Directories:
    Quickly toggle between the current directory and the last directory on the stack:

  3. Using with +n:
    Switch to the third directory in the stack:

    pushd +2

Common Issues

  • Stack Overflow: pushd uses a stack to store directory paths; if too many directories are pushed without any being popped, it can lead to excessive memory usage.

    Solution: Regularly use popd to remove directories from the stack when they are no longer needed.

  • Invalid Directory: Attempting to push an invalid or unreachable directory.

    Solution: Always check the directory path validity before using pushd.


pushd can be integrated with other commands for script automation. For example, navigating directories to perform operations and then returning back:

pushd /tmp
# Execute commands in /tmp
# Returns to the original directory

An advanced script might involve logging:

pushd /var/log
grep "error" *.log > ~/errors_found.txt
  • popd: Pops the top directory off the stack and returns you to it.
  • dirs: Displays the list of currently remembered directories.

Further reading and more details can be found in the bash manual or the online GNU documentation (man bash or info bash) for shell built-ins.