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

pushd and popd are commands that allow you to work with a directory stack and change the current working directory in Linux and other Unix-like operating systems.
Instead of typing long paths with cd
each time you need to switch directories, 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 efficiently.
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.
The current working directory
is always at the top of the stack (position 0). Each time you interact with the command line, you are working within a directory. The pwd
command shows which directory that is.
When navigating through the file system, use the Tab key to autocomplete directory names. Adding a slash at the end of the directory name is optional.
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 [OPTIONS] [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. Because the current directory must always be on the top of the stack, once changed the new current directory goes to the top of the stack but it is not saved in the stack. To save it, you must invoke pushd from it. If you use cd to change to another directory, the top item of the stack will be lost.
Here is another example. To add /opt to the stack:
pushd /opt/opt /var/www ~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 +2Or 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 behaves the same as cd -.
popd Command
The popd command takes the form:
popd [OPTIONS]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 ~Removing a Specific Entry
Like pushd, popd accepts +N and -N to remove a specific entry by its position:
popd +1/usr/local ~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 entire 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 clear the stack and start fresh:
dirs -cPractical 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:
pushd /var/www/mysite
/var/www/mysite ~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 ~pushd
/etc/nginx/sites-available /var/www/mysite ~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:
pushd ~/project/src
~/project/src ~pushd ~/project/build
~/project/build ~/project/src ~pushd /var/www/html
/var/www/html ~/project/build ~/project/src ~Jump back to ~/project/src directly:
pushd +2Quick 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) |
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 |
dirs | Display the directory stack |
dirs -v | Display the stack as a numbered list |
dirs -l | Show full paths (expand ~) |
dirs -c | Clear the directory stack |
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.
Can I use pushd and popd in scripts?
Yes. They are useful in shell scripts that need to temporarily change to another directory and then return. A common pattern is pushd /some/dir > /dev/null, followed by your commands, then popd > /dev/null to return. Redirecting to /dev/null suppresses the stack output.
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 entries from the stack.
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.
i am
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 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page