Skip to content
Related Articles

Related Articles

Improve Article
popd Command in Linux with Examples
  • Last Updated : 02 Nov, 2020

popd command is used to remove directories from the directory stack. The “d” in popd stands for the directory as it removes the directory path onto the stack. After this command is executed, the present directory stack is displayed as a list of space-separated directories. The directory stack decreases in size after each popd command. This directory stack is based on the Last In First Out (LIFO) principle.

The syntax for popd command:

popd [OPTIONS] [DIRECTORY]

Working with popd command

Consider the following directory stack:

#use the following command to view the directory stack 
dirs -l -v

directory stack

1. Deleting directories in the directory stack

Without any argument, popd command removes the top directory from the stack. The directory at the second from the top becomes the current directory after the top directory is removed. Referring to the image below, it can be seen that before the execution of popd command, the current directory is ‘Desktop'(as it is the top directory on the stack) but after the command execution, the current directory is changed to “~” which represents the home directory as it was in the second place.



deleting directories in directory stack

2. Deleting a directory from the stack without changing the current directory

Removal of a directory without changing the current directory can be done by using ā€œ-nā€ along with popd command. By using this command, the directory which is in the second position from the top is removed.

popd -n

Referring to the image below, it can be seen that the home directory remains the current directory and the directory in the second place from the top is removed.

Deleting a directory from the stack without changing the current directory

3. Removal of a directory from any position:

Removal of a directory can be done from any position. A numerical parameter representing the position of the directory in the directory stack can be passed along with popd command.

popd +N #N is the numerical parameter
popd -N #N is the numerical parameter

When popd +N is used, the Nth directory is deleted from the top. When popd -N uses the Nth directory from the bottom is deleted.

Removal of a directory from any position:

Note: If there is only one directory in the directory stack, then an error is displayed as there has to be one current working directory.

Removal of a directory from any position:

My Personal Notes arrow_drop_up
Recommended Articles
Page :