popd
Return to previously pushed directory
TLDR
Remove the top directory from the stack and cd to it
Remove the Nth directory (starting from zero to the left from the list printed with dirs)
Remove the Nth directory (starting from zero to the right from the list printed with dirs)
Remove the 1st directory (starting from zero to the left from the list printed with dirs)
SYNOPSIS
popd [+n]
PARAMETERS
+n
Removes the nth directory (counting from the left of the directory stack as shown by dirs
command) starting with zero. The directories are numbered from zero starting at the first directory listed by dirs
; i.e., popd +0
is equivalent to popd
. The directory to be popped is automatically changed to the new top of the stack.
DESCRIPTION
The popd
command in Linux is used to remove the top directory from the directory stack and change the current working directory to the directory now at the top of the stack. Think of the directory stack as a 'last-in, first-out' (LIFO) structure. popd
complements the pushd
command, which adds directories to this stack.
This command provides a quick and convenient way to navigate back through a series of directories that you have visited. It's particularly useful when working on projects that involve frequently switching between multiple directories. The directory stack is maintained separately for each shell session. Using popd
without any arguments simply removes the top entry and changes to that directory. With options it's possible to manipulate elements within the stack.
CAVEATS
Using popd
without any directories on the stack will result in an error.
DIRECTORY STACK
The directory stack is a data structure maintained by the shell. It stores a list of directory paths. pushd
adds a new directory to the top, while popd
removes the top directory and changes to it.
EXAMPLE USAGE
Imagine you're in /home/user/project1
.
You run pushd /tmp
, changing to /tmp
and adding /home/user/project1
to the stack.
Running popd
will remove /home/user/project1
from the stack and change your directory back to /home/user/project1
.
HISTORY
The pushd
, popd
, and dirs
commands are shell built-ins, primarily associated with shells like bash
and zsh
. They were developed to improve directory navigation in interactive shell sessions, offering a more structured approach compared to repeatedly using cd
.