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
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.
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.
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.
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.
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!