Oct 15, 2023 4 min read

Pushd and Popd Commands in Linux

Use pushd and popd commands in linux with our step-by-step tutorial. It allows you to interact with directory stacks and modify current directory

Pushd and Popd Commands in Linux
Table of Contents

Introduction

Before we start discussing about pushd and popd commands in linux, let's first understand-What is a Pushd and Popd Command ?

In Linux and other Unix-like operating systems, the commands pushd and popd allow you to interact with directory stacks and modify the current working directory. Despite the fact that pushd and popd are extremely powerful and helpful commands, they are underappreciated and underutilized.

In this tutorial, we'll show you how to browse your system's directory tree with the pushd and popd commands.

Directory Stack

The directory stack is a list of the directories you've visited previously. The dirs command displays the contents of the directory stack. When using the pushd command to change to a directory, directories are added to the stack, and when using the popd command, they are deleted.

The top of the directory stack is always the current working directory. The user's current working directory is the folder (directory) in which he or she is now working. You're working in a directory every time you interact with the command line.

You can use the pwd command to find out what directory you're in right now.

Use the Tab key to autocomplete directory names when moving around the file system. It's not necessary to include a slash at the end of the directory name.

The behavior of pushd, popd, and dirs is shell-specific, and it may alter slightly from shell to shell. The built-in version of the commands in Bash will be covered.

pushd Command

The pushd command has the following syntax:

pushd [OPTIONS] [DIRECTORY]

To move the current directory to the top of the directory stack and shift to /var/www, for instance, type:

pushd /var/www

The command above will print the directory stack if it succeeds. ~ is the directory in which the pushd command will run. The home directory is represented by the tilde ~ symbol.

/var/www ~

Pushd navigates to the provided directory after saving the current working directory to the top of the stack. Because the current directory must always be at the top of the stack, when it is changed, the new current directory is added to the top of the stack but not stored. You must use pushd from it to save it. The top item in the stack will be lost if you use cd to change directories.

Let's add another folder to the mix:

/var/www$ pushd /opt
Output

/opt /var/www ~

Use the -n option to prevent directory changes. To add the /usr/local directory to the stack without changing into it, for example, type:

/opt$ pushd -n /usr/local

The /usr/local directory is inserted second from the top of the stack since the current directory (which is always at the top) is not changed:

Output

/opt /usr/local /var/www ~

The pushd command accepts two parameters, +N and -N, which allow you to travel to the stack's Nth directory. The +N option switches the stack list's Nth member from left to right, starting at zero. When -N is used, the count is performed from right to left.

Let's print the current directory stack to further illustrate the options:

/opt$ dirs -l -v

An indexed list of the directory stack will appear in the output:

Output

0  /opt
1  /usr/local
2  /var/www
3  /home/linuxize

One of the following commands will change to the /var/www directory and bring it to the top of the stack.

The directory's index is 2 when counting from top to bottom (or left to right).

pushd +2

The index of the /var/www directory is 1 when counting from bottom to top.

pushd -1

Pushd will flip the top two directories and make the new top the current directory when used without any arguments. When you use the cd - command, the result is the same.

popd Command

The popd command has the following format:

popd [OPTIONS]

Popd removes the top directory from the stack and navigates to the new top directory when used without an argument.

Let's pretend we've got the following directory structure:

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

When you run the popd command, the /opt directory is removed from the stack and replaced with the /usr/local directory:

/opt$ popd

The new directory stack will be displayed in the output:

Output

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

The -n option disables the automatic directory switch and removes the second item from the stack:

/opt$ popd -n
Output

/usr/local /etc/nginx ~

Popd, like pushd, takes the +N and -N arguments, which can be used to remove the stack's Nth directory.

/opt$ popd +1
/usr/local ~

FAQs on Pushd and Popd Command in Linux

How does the directory stack work with pushd and popd?

The directory stack is a Last-In-First-Out (LIFO) data structure that keeps track of directories as they are pushed and popped. Each time pushd is used, the current directory is changed, and the previous directory is added to the stack. popd removes directories from the stack and changes the current directory to the top directory in the stack.

How do I use the pushd command to change directories?

To use pushd, simply provide the target directory as an argument. For example, pushd /path/to/directory will change the current directory to /path/to/directory and add the previous directory to the stack.

Can I use pushd without specifying a target directory?

Yes, if you use pushd without any arguments, it will just rotate the top two directories in the stack, making the second directory the current directory and the first directory the new top of the stack.

What is the purpose of the popd command in Linux?

The popd command is used to remove directories from the directory stack and change the current directory.

How do I use the popd command to change directories?

To use popd, simply run the command without any arguments. It will remove the top directory from the stack and change the current directory to the new top of the stack.

Can I use popd to remove directories from the stack without changing the current directory?

Yes, if you use popd with the -n option, it will remove the top directory from the stack without changing the current directory.

Is it possible to view the contents of the directory stack?

Yes, you can use the dirs command to view the contents of the directory stack. By running dirs without any arguments, it will display the directories in the stack in the order they were added.

Conclusion

To transfer from one directory to another, you would normally use the cd command. If you spend a lot of time on the command line, however, the pushd and popd commands will help you to be more productive and efficient.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.