popd – Remove directories from the directory stack

The popd command is used to remove directories from the directory stack. The directory stack is a list of directories that are stored in memory and can be accessed and manipulated using various commands, including pushd and popd. The popd command removes the top directory from the stack and changes the current working directory to the new top directory.

Overview

The syntax for the popd command is as follows:

popd [-n] [+N | -N]

The options and arguments for the popd command are as follows:

  • -n: By default, popd also changes the current working directory to the new top directory. The -n option prevents this behavior.
  • +N or -N: Removes the Nth directory from the stack. +N removes the directory at position N from the left (starting from 0), while -N removes the directory at position N from the right (starting from 1).

Examples

To remove the top directory from the stack and change the current working directory to the new top directory, simply type:

popd

To remove the second directory from the stack and change the current working directory to the new top directory, type:

popd +1

To remove the third directory from the stack without changing the current working directory, type:

popd -n +2

Options

The available options for the popd command are as follows:

Option Description
-n Do not change the current working directory.
+N Remove the directory at position N from the left (starting from 0).
-N Remove the directory at position N from the right (starting from 1).

Troubleshooting tips

If you receive an error message when using the popd command, it may be because the directory stack is empty. To check the contents of the directory stack, use the dirs command. If the directory stack is empty, you will need to use the cd command to change the current working directory.

Notes

When used in combination with the pushd command, the popd command can be a powerful tool for navigating and manipulating directories in Linux. However, it is important to use these commands with caution, as they can be confusing and may lead to unexpected results if used incorrectly.