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

By 

Updated on

6 min read

Terminal showing pushd, popd, and dirs command examples in Linux

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:

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

Terminal
pushd /var/www

On success, the command prints the directory stack. ~ is the directory in which we executed the pushd command. The tilde symbol ~ means home directory.

output
/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:

Terminal
pushd /opt
output
/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:

Terminal
pushd -n /usr/local

Because the current directory (which is always at the top) does not change, /usr/local is added second from the top:

output
/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:

Terminal
dirs -l -v
output
 0  /opt
 1  /usr/local
 2  /var/www
 3  /home/linuxize

To change to /var/www and bring it to the top, you can count from the left (index 2):

Terminal
pushd +2

Or count from the right (index 1):

Terminal
pushd -1

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

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

output
/opt /usr/local /var/www /etc/nginx ~

Running popd removes /opt from the stack and changes to /usr/local:

Terminal
popd
output
/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:

Terminal
popd -n
output
/usr/local /etc/nginx ~

Removing a Specific Entry

Like pushd, popd accepts +N and -N to remove a specific entry by its position:

Terminal
popd +1
output
/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:

Terminal
dirs
output
/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:

Terminal
dirs -l -v
output
 0  /opt
 1  /usr/local
 2  /var/www
 3  /home/linuxize

To clear the stack and start fresh:

Terminal
dirs -c

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

Terminal
pushd /var/www/mysite
/var/www/mysite ~
Terminal
pushd /etc/nginx/sites-available
/etc/nginx/sites-available /var/www/mysite ~

Now toggle between them with pushd (no arguments):

Terminal
pushd
/var/www/mysite /etc/nginx/sites-available ~
Terminal
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:

Terminal
pushd ~/project/src
~/project/src ~
Terminal
pushd ~/project/build
~/project/build ~/project/src ~
Terminal
pushd /var/www/html
/var/www/html ~/project/build ~/project/src ~

Jump back to ~/project/src directly:

Terminal
pushd +2

Quick Reference

CommandDescription
pushd /pathSave current directory and change to /path
pushdToggle between the top two directories
pushd -n /pathAdd /path to stack without changing to it
pushd +NRotate stack and change to the Nth entry (from left)
pushd -NRotate stack and change to the Nth entry (from right)
popdRemove top directory and change to the new top
popd -nRemove second entry without changing directory
popd +NRemove the Nth entry from the stack
dirsDisplay the directory stack
dirs -vDisplay the stack as a numbered list
dirs -lShow full paths (expand ~)
dirs -cClear 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

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