pushd and popd Commands in Linux: Navigate Directories with a Stack

When you work on a server, you often move between the same few directories: a project tree, a configuration directory under /etc, and a log directory. Typing the full path with cd
every time is slow, and cd - only remembers the last directory you left.
pushd and popd keep a stack of directories for you. pushd saves your current location and moves you to a new one. When you are ready to go back, popd returns you to the saved directory. This is especially useful when working across several directories in a single session.
This guide explains how to use pushd, popd, and dirs to navigate your system’s directory tree, both at the prompt and inside shell scripts.
Directory Stack
The directory stack is a list of directories you have previously navigated to. Directories are added to the stack with pushd and removed with popd. You can view the stack at any time with the dirs command.
Each popd removes the current directory from the stack and returns you to the most recently saved previous directory.
The current working directory
is always at the top of the stack (position 0). The pwd command shows which directory that is.
pushd, popd, and dirs are shell builtins, and their behavior may differ slightly from shell to shell. This guide covers the Bash builtin versions.
pushd Command
The syntax for the pushd command is as follows:
pushd [-n] [+N | -N | DIRECTORY]For example, to save the current directory to the top of the directory stack and change to /var/www, you would type:
pushd /var/wwwOn success, the command prints the directory stack. ~ is the directory in which we executed the pushd command. The tilde symbol ~ means home directory.
/var/www ~pushd first saves the current working directory to the top of the stack and then navigates to the given directory. The top entry always mirrors the current directory. If you use cd to change to another directory, the top entry is replaced with the new one, and the directory it held is lost. To keep it on the stack, change directories with pushd instead.
Here is another example. To add /opt to the stack:
pushd /opt/opt /var/www ~/opt is now on top and is your current directory, and /var/www moved down one position.
Adding Without Changing Directory
To add a directory to the stack without changing into it, use the -n option. For example, to add /usr/local to the stack but stay in /opt:
pushd -n /usr/localBecause the current directory (which is always at the top) does not change, /usr/local is added second from the top:
/opt /usr/local /var/www ~Rotating the Stack
pushd accepts +N and -N options that let you rotate the stack and jump to a specific directory. +N counts from left to right (top to bottom) starting at zero. -N counts from right to left (bottom to top).
To see the indexed stack, use dirs -l -v:
dirs -l -v 0 /opt
1 /usr/local
2 /var/www
3 /home/linuxizeTo change to /var/www and bring it to the top, you can count from the left (index 2):
pushd +2/var/www ~ /opt /usr/local/var/www is now the current directory. The two entries that were above it, /opt and /usr/local, moved to the bottom of the stack in the same order, and nothing was removed.
Starting from the same original stack, you can also count from the right (index 1):
pushd -1Both commands rotate the stack so that /var/www becomes the current directory.
When used without any argument, pushd toggles the top two directories and makes the new top the current directory. This is similar to cd -, but the two read from different places. cd - returns to the directory stored in $OLDPWD, and pushd uses the second entry on the stack. After a plain cd, they can point to different directories.
popd Command
The popd command takes the form:
popd [-n] [+N | -N]When used with no argument, popd removes the top directory from the stack and navigates to the new top directory.
Suppose we have the following directory stack:
/opt /usr/local /var/www /etc/nginx ~Running popd removes /opt from the stack and changes to /usr/local:
popd/usr/local /var/www /etc/nginx ~Removing Without Changing Directory
The -n option suppresses the directory change and removes the second item from the stack instead of the first:
popd -n/usr/local /etc/nginx ~/var/www is gone from the stack, and you are still in /usr/local.
Removing a Specific Entry
Like pushd, popd accepts +N and -N to remove a specific entry by its position:
popd +1/usr/local ~popd +1 removed /etc/nginx, the entry at index 1. When you are not sure which index belongs to which directory, check with dirs -v first.
dirs Command
The dirs command displays the directory stack. By default it prints the stack on a single line with directories separated by spaces:
dirs/opt /usr/local /var/www ~Useful options for dirs:
-v- Display the stack as a numbered list, one entry per line-l- Expand~to the full home directory path-c- Clear the directory stack-p- Print one entry per line, without index numbers
Combining -l and -v gives you the most readable view:
dirs -l -v 0 /opt
1 /usr/local
2 /var/www
3 /home/linuxizeTo print a single entry, pass its index. For example, to print the second directory on the stack:
dirs +1/usr/localTo clear the stack and start fresh:
dirs -cdirs -c removes every saved entry. Running dirs afterwards still prints one entry, the directory you are in.
Referring to Stack Entries
Bash expands a few tilde prefixes into directories from the stack. You can use them in any command without typing the full path:
~N- The Nth entry counting from the left, the same asdirs +N~-N- The Nth entry counting from the right, the same asdirs -N~+- The current directory, the same as$PWD~-- The previous directory, the same as$OLDPWD
Suppose the stack looks like this:
~/project/build ~/project/src ~To copy app.conf from the source directory into the build directory you are in, you would type:
cp ~1/app.conf .Bash expands ~1 to /home/linuxize/project/src before running cp, and you stay in the build directory the whole time.
Bash also keeps the stack in the DIRSTACK array
. Each element holds the full path of one entry:
echo "${DIRSTACK[@]}"/home/linuxize/project/build /home/linuxize/project/src /home/linuxizePractical Examples
Switching Between Project and Config Directories
When you are working on a project and need to repeatedly edit configuration files, pushd lets you switch between the two directories without retyping paths.
First, save your location and move to the project directory:
pushd /var/www/mysite/var/www/mysite ~Then move to the Nginx configuration directory:
pushd /etc/nginx/sites-available/etc/nginx/sites-available /var/www/mysite ~Now toggle between them with pushd (no arguments):
pushd/var/www/mysite /etc/nginx/sites-available ~Run it again to go back to the configuration directory:
pushd/etc/nginx/sites-available /var/www/mysite ~Each call swaps the top two entries. Your home directory stays at the bottom of the stack.
Building a Multi-Directory Workflow
If you are working across several directories (for example, source code, build output, and deployment target), you can build a stack and jump to any entry by index.
Start with the source directory:
pushd ~/project/src~/project/src ~Add the build directory:
pushd ~/project/build~/project/build ~/project/src ~Finally, add the deployment target:
pushd /var/www/html/var/www/html ~/project/build ~/project/src ~Jump back to ~/project/src directly:
pushd +2~/project/src ~ /var/www/html ~/project/build~/project/src is now the current directory, and the other three entries are still on the stack. Running pushd +2 again takes you to /var/www/html.
Using pushd and popd in Scripts
pushd and popd are useful in shell scripts that need to work in another directory for a while and then return to where they started. Both commands print the stack on every call. In a script, that output is noise, so redirect it to /dev/null
:
#!/bin/bash
# Stop the script if the directory change fails
pushd /var/www/mysite > /dev/null || exit 1
git pull || exit 1
npm run build || exit 1
popd > /dev/nullThe || exit 1 after pushd stops the script if the directory change fails. Without that check, git pull and npm run build would run in the directory the script started from. The same check after git pull and npm run build stops the script if either command fails.
Bash has no option to silence pushd. If you try the -q flag that zsh supports, Bash treats it as an invalid index:
pushd -q /var/wwwbash: pushd: -q: invalid number
pushd: usage: pushd [-n] [+N | -N | dir]Redirecting the output hides only the stack listing. Error messages go to standard error and still appear in the terminal or the script log.
When the directory change covers a single command, a subshell
is simpler. (cd /var/www/mysite && npm run build) runs the build there and leaves the script in its original directory.
Troubleshooting
bash: popd: directory stack empty
The stack holds only the current directory, so there is nothing for popd to remove. Run dirs -v to see what the stack contains.
bash: pushd: no other directorypushd without arguments swaps the top two entries, and the stack has only one. Add a directory first with pushd /path.
bash: pushd: /path: No such file or directory
The directory does not exist, or the path contains a typo. The stack is left unchanged, so check the path with ls -ld /path and run pushd again.
popd fails on a directory added with pushd -npushd -n does not check that the directory exists. A mistyped path is saved to the stack, and the error appears only when popd tries to change into it. The bad entry stays on the stack until you remove it with popd -n or popd +N.
sh: 1: pushd: not found
The script runs under sh, not Bash. On Ubuntu and Debian, /bin/sh is dash, which does not have pushd or popd. Start the script with a #!/bin/bash shebang
, or run it with bash script.sh.
Quick Reference
| Command | Description |
|---|---|
pushd /path | Save current directory and change to /path |
pushd | Toggle between the top two directories |
pushd -n /path | Add /path to stack without changing to it |
pushd +N | Rotate stack and change to the Nth entry (from left) |
pushd -N | Rotate stack and change to the Nth entry (from right) |
pushd /path > /dev/null | Change directory without printing the stack |
popd | Remove top directory and change to the new top |
popd -n | Remove second entry without changing directory |
popd +N | Remove the Nth entry from the stack (from left) |
popd -N | Remove the Nth entry from the stack (from right) |
dirs | Display the directory stack |
dirs -v | Display the stack as a numbered list |
dirs -l | Show full paths (expand ~) |
dirs -p | Display one entry per line |
dirs +N | Display the Nth entry |
dirs -c | Clear the directory stack |
~N | Use the Nth stack entry in a command |
FAQ
What is the difference between pushd and cd?cd changes your working directory but does not remember the previous one (except for cd -, which remembers only the last directory). pushd saves your current directory to a stack before changing, so you can return to any saved directory with popd.
How do I stop pushd from printing the stack?
Redirect its output to /dev/null, for example pushd /some/dir > /dev/null. The same works for popd. Bash has no quiet flag for these commands, and errors are still printed.
Does the directory stack persist across terminal sessions?
No. The directory stack exists only in the current shell session. When you close the terminal or open a new one, the stack starts empty.
How do I clear the directory stack?
Run dirs -c to remove all saved entries. The current directory remains listed.
Conclusion
The pushd and popd commands let you build a stack of directories and switch between them without retyping paths. Combined with dirs for viewing the stack, they offer a faster alternative to cd
when working across multiple directories.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page