dirs
Display the directory stack
TLDR
Display the directory stack with a space between each entry
Display the directory stack with one entry per line
Display a numbered list of entries in the directory stack
Display the directory stack without the tilde-prefix (~)
Display only the nth entry in the directory stack, starting at 0 (Bash only)
Display only the nth entry in the directory stack from the last, starting at 0 (Bash only)
Clear the directory stack
SYNOPSIS
dirs [-clpv] [+N] [-N]
PARAMETERS
-c
Clears the entire directory stack, removing all entries.
-l
Produces a listing using full pathnames, suppressing tilde (~) expansion for the home directory.
-p
Prints the directory stack with each entry on a new line.
-v
Prints the directory stack with one entry per line, prefixed by its 0-based index in the stack.
+N
Displays the Nth entry from the left (0-indexed) of the directory stack. '0' refers to the current directory.
-N
Displays the Nth entry from the right (0-indexed) of the directory stack.
DESCRIPTION
The dirs command is a shell built-in utility designed to display the current contents of the directory stack. This stack is a list of directories that can be manipulated using commands like pushd and popd, allowing users to navigate efficiently between recently visited directories without manually typing their full paths.
When executed without any options, dirs presents the stack with pathnames abbreviated, typically replacing the home directory with a tilde (~). Various options allow for different display formats, such as showing full pathnames, numbering the entries, or listing one directory per line. Additionally, dirs can be used to clear the entire directory stack, effectively resetting its state.
CAVEATS
dirs is a shell built-in command (e.g., in Bash, Zsh, Tcsh), meaning it is part of the shell itself rather than a standalone executable found in directories like /bin or /usr/bin. Its behavior and availability are thus dependent on the specific shell being used.
The directory stack is ephemeral; it exists only for the current shell session and is not persistent across different terminal windows or after the shell exits.
DIRECTORY STACK INDEXING
The directory stack is 0-indexed. When using +N or -N, '0' consistently refers to the current working directory, which is always the first entry in the stack as displayed by dirs without options. Subsequent numbers refer to the directories further back in the stack history.
HISTORY
The concept of a directory stack, along with complementary commands like pushd, popd, and dirs, was first introduced with the C shell (csh). This innovation aimed to provide a more dynamic and efficient way to navigate directories. Due to its utility, this feature was subsequently adopted by other prominent Unix-like shells, including Bash and Zsh, becoming a standard and highly valued component of shell functionality for both interactive use and scripting.