Linux pushd and popd command examples

8

The pushd and popd commands allow you to work with directory stacks in Linux and Unix-like operating systems. They are used to add and remove directories from your directory stack. The “d” in pushd and popd stands for the directory.

The concept of stacks is straightforward. The directory stack is a Last In, First Out(LIFO) queue. After each popd command is executed, the directory stack decreases in size.

This tutorial will learn how to use the pushd and popd commands to navigate the Linux directory tree.

Basic Syntax

The pushd command takes the syntax below:

$ pushd [OPTIONS] [DIRECTORY]

Below are examples of how to use the pushd command.

Add directories using the pushd command

The pushd command is used to add directories onto the directory tree stack. The command below adds the Music directory onto the stack.

From the image below, the Music directory now becomes the current working directory.

To view your directory stack, run:

When you add directories to the stack, the most recent added directory will be at the top of the stack.

Add a new directory without modifying the current directory

When you execute the pushd command, the current directory is changed to the one you have specified. To add a new directory while keeping the current directory unchanged, use the -n option.

When you execute the command, the directory will now be at the second position in the stack.

Use the  -n option together with the directory you want to add, as shown in the syntax below.

From the image below, the Pictures directory is now in the second position from the top.

Move to a directory at any position in the stack

The pushd command accepts numeric parameters. It allows you to navigate to the nth directory in the stack. The  +n  option starts from the top of the stack, and the directory in the nth position will now become the current directory.

From the image below, the directory at the +2 position is the ‘Videos’ directory. After pushd is executed, the Videos directory is now at the top of the stack.

Popd command

The popd command sets the current directory to the directory recently stored by the pushd command. Each time you invoke the pushd command, a directory is stored.

Basic Syntax

Popd command takes the syntax below

$ popd [OPTIONS] [DIRECTORY]

Delete directories in the directory stack

On its own, the popd command allows you to remove the directory at the top of your directory stack. After the command is executed, the second directory from the top now becomes the current directory.

To view your directory stack use the command:

Next, run the popd command:

Let’s refer to the image shown below. In my case, the home directory is at the top of my directory stack. After the popd command execution,  the home directory is purged, and the directory at the top becomes the Videos directory.

Delete a directory from the stack using the -n option

The popd command and the -n option can delete a directory on the stack without modifying your current directory.

When you execute the popd -n command, the directory at the second position from the top of your stack is removed.

Referring to the image below, after the command is executed, the home directory remains at the top of the stack. In contrast, the directory in the second position from the top is removed.

Delete a directory from any position

You can remove a directory in the nth position. A numerical parameter is passed along with the popd command.

Syntax:

When you execute the popd +n command, the directory in the nth position from the top of the stack is deleted. The option -n deletes the directory in the nth position starting from the bottom of the stack.

From the image below, we executed the command popd +1. This removes the Downloads directory, which is in the first position from the top of the stack.  Each directory then moves up one place in the stack.

Option -0 allows you to delete the last directory on the stack.  Run the command below:

From the image below, the desktop directory is removed from the stack. 

Conclusion

As you have observed, the pushd and popd commands are useful in navigating from one directory to another. As you get used to the command, you will have a faster and efficient way of navigating through your directories in Linux.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More