
popd Command in Linux
The popd command in Linux removes the top entry from the directory stack and changes the current directory to the new top of the stack. It is a built-in shell command that simplifies directory navigation by allowing quick switching back to previous directories. It operates on the last in, first out (LIFO) principle. Note that the popd only works if the directory stack is non-empty.
Table of Contents
Here is a comprehensive guide to the options available with the popd command â
Syntax of popd Command
The syntax of the popdcommand in Linux is as follows:
popd [options]
The command removes the top directory from the stack and changes the current directory to the next entry in the stack. The optional arguments allow modifying the stack without switching directories.
Options of popd Command
The arguments of the Linux popd command are listed below:
Option | Description |
---|---|
+N | Removes the Nth directory (counting from the left in the dirs output, starting at zero) from the stack. |
-N | Removes the Nth directory (counting from the right in the dirs output, starting at zero) from the stack. |
Examples of popd Command in Linux
This section discusses how to use the popd command in Linux with examples:
Removing the Top Directory from the Stack
To remove the top directory from the stack and switch to the next one, use the popd command as follows:
popd
This command removes the current top directory from the stack and changes to the new top directory. To display the stack, use the following command:
dirs -v -l

The directory at 0 indicates the current working directory.
Note: The popd command returns an error if the stack is empty.
Removing a Specific Directory from the Stack
By default, the popd command removes the directory at the top of the stack. To remove a specific directory from the stack without changing it, use the +N argument with the popd command. For example, to remove the directory at the index 1, use the command given below:
popd +1
To verify, execute the dirs command:
dirs -v âl

As can be seen from the output image before removing the specific directory the current working directory is /etc and after removing the directory the current working directory is still /etc.
Removing a Directory from the End of the Stack
To remove a directory from the right side of the stack, use the -N argument. Executing the following command removes the second last directory in the stack:
popd -1
To verify, execute the dirs command −
dirs -v âl

Comparison Table
Option | Description | Example |
---|---|---|
+N | Removes the Nth directory (counting from the left) | popd +1 |
-N | Removes the Nth directory (counting from the right) | popd -1 |
Conclusion
The popd command in Linux simplifies directory navigation by removing the top directory from the stack and switching to the next available entry. It follows a last in, first out (LIFO) structure, making it efficient for returning to previous directories. The command supports options for removing specific directories without changing locations.
Practical examples include removing the most recent directory, removing a particular directory by index, and handling directory stack operations efficiently. Using popd along with dirs helps manage directory history, improving workflow efficiency in a shell environment.