LinuxCommandLibrary

popd

Return to previously pushed directory

TLDR

Remove the top directory from the stack and cd to it

$ popd
copy

Remove the Nth directory (starting from zero to the left from the list printed with dirs)
$ popd +N
copy

Remove the Nth directory (starting from zero to the right from the list printed with dirs)
$ popd -N
copy

Remove the 1st directory (starting from zero to the left from the list printed with dirs)
$ popd -n
copy

SYNOPSIS

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

PARAMETERS

+N
    Removes the Nth directory from the top of the stack. Directories are numbered starting from 0, where +0 is the current directory, +1 is the first directory on the stack, and so on. The shell then changes to the new top of the stack (which was previously +1, unless +0 was removed).

-N
    Removes the Nth directory from the bottom of the stack. Directories are numbered starting from 0, where -0 is the bottom of the stack, -1 is the next to bottom, and so on. The shell then changes to the new top of the stack.

-n
    Suppresses the normal change of directory when a directory is popped off the stack. The directory is removed from the stack, but the current working directory remains unchanged. Also suppresses the display of the directory stack after the operation. This option is specific to Bash and Zsh.

DESCRIPTION

The popd command is a shell builtin that manipulates the directory stack. It removes an entry from the stack, typically the top-most directory, and then changes the current working directory to the new top of the stack. This command is commonly used in conjunction with pushd, which adds directories to the stack, and dirs, which displays the stack's contents.

When executed without arguments, popd removes the directory currently at the top of the stack (the most recently pushed directory) and makes the directory that was previously second from the top the new current working directory.

Arguments allow for more specific manipulation, enabling the removal of directories at particular positions within the stack or preventing the automatic directory change.

CAVEATS

popd is a shell builtin command, meaning its exact behavior and available options can vary slightly between different shells (e.g., Bash, Zsh, Fish). The core functionality remains consistent across modern shells that support a directory stack.

If the directory stack becomes empty after a popd operation, the current working directory remains unchanged. Attempting to pop from an empty stack or specifying an invalid index (N out of bounds) will result in an error message from the shell.

THE DIRECTORY STACK

The directory stack is a list of directories maintained by the shell. It allows users to save and recall previously visited directories, facilitating quick navigation without having to type full paths repeatedly. pushd adds directories to this stack, popd removes them, and dirs displays the current contents of the stack.

COMMON USAGE PATTERN

popd is almost always used in conjunction with pushd and dirs. A common workflow involves using pushd to 'bookmark' directories as you navigate, using dirs to see your 'bookmarks', and then using popd to quickly return to a previously pushed directory. For example, pushd /path/to/project1, then pushd /path/to/config, then popd to return to /path/to/project1.

HISTORY

The concept of a directory stack, along with commands like pushd, popd, and dirs, originated in the C shell (csh) and was later adopted by other popular shells, including tcsh and Bash. This feature was introduced to provide a more efficient and flexible way for users to navigate frequently visited directories, improving productivity for developers, system administrators, and anyone who regularly switches between different project or configuration paths.

SEE ALSO

pushd(1), dirs(1), cd(1)

Copied to clipboard