popd Command in Linux with Examples
Last Updated :
11 Oct, 2024
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.
Syntax
popd [OPTIONS] [DIRECTORY]
where,
- OPTIONS: Various flags to modify the behavior of the command.
- DIRECTORY: Optional parameter indicating a specific directory to remove from the stack.
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.
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.

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.

Conclusion
The popd command is a powerful yet often underutilized tool in Linux for managing the directory stack. It allows for efficient directory navigation, stack manipulation, and script automation. By mastering the use of popd along with the complementary pushd and dirs commands, users can greatly enhance their productivity in the command line environment. Understanding the various options, such as -n, +N, and -N, can further customize how directories are removed from the stack, providing flexibility and control over your navigation.
Similar Reads
od command in Linux with example The od (octal dump) command in Linux is a versatile tool used to display file contents in various formats, with the default being octal. This command is particularly useful for debugging scripts, examining binary files, or visualizing non-human-readable data like executable code. It allows users to
6 min read
pmap command in Linux with Examples The pmap command in Linux is a powerful utility used to display the memory map of a process. A memory map provides insight into how memory is allocated and distributed within a running process. This can be incredibly useful for developers and system administrators when debugging memory issues, optim
3 min read
pushd Command in Linux with Examples The 'pushd' command is a built-in shell command that simplifies the navigation and management of directories by using a directory stack. This stack operates on the Last In, First Out (LIFO) principle, allowing users to push directories onto the stack and easily switch between them. By appending a di
4 min read
read command in Linux with Examples read command in the Linux system is used to read from a file descriptor. This command reads up the total number of bytes from the specified file descriptor into the buffer. If the number or count is zero, this command may detect errors. But on success, it returns the number of bytes read. Zero indic
3 min read
printf command in Linux with Examples The 'printf' command in Linux is a versatile tool used to display formatted text, numbers, or other data types directly in the terminal. Like the 'printf' function in programming languages like C, the Linux printf command allows users to format output with great precision, making it ideal for script
4 min read
script command in Linux with Examples The 'script' command in Linux is a versatile tool that allows you to record all terminal activities, including inputs and outputs, making it a valuable resource for developers, system administrators, educators, and anyone who needs to document terminal sessions. This command captures everything disp
5 min read