LinuxCommandLibrary

dirs

Display the directory stack

TLDR

Display the directory stack with a space between each entry

$ dirs
copy

Display the directory stack with one entry per line
$ dirs -p
copy

Display a numbered list of entries in the directory stack
$ dirs -v
copy

Display the directory stack without the tilde-prefix (~)
$ dirs -l
copy

Display only the nth entry in the directory stack, starting at 0 (Bash only)
$ dirs +[n]
copy

Display only the nth entry in the directory stack from the last, starting at 0 (Bash only)
$ dirs -[n]
copy

Clear the directory stack
$ dirs -c
copy

SYNOPSIS

dirs [-clpv] [+N] [-N]

PARAMETERS

-c
    Clear the directory stack by deleting all entries

-l
    Use full pathnames instead of relative paths or ~

-p
    Print one directory per line

-v
    Verbose mode: one entry per line with full pathnames

+N
    Display the Nth entry from the top (1-based, +1 is top)

-N
    Display the Nth entry from the bottom (1-based, -1 is bottom)

DESCRIPTION

The dirs command is a Bash shell built-in that shows the list of directories in the shell's directory stack. This stack is managed by pushd (which adds directories and rotates the stack) and popd (which removes directories). By default, dirs prints the stack starting from the topmost directory (current directory) followed by previous ones, using tilde (~) notation where possible and relative paths.

It is useful for navigating complex directory structures without repeatedly typing full paths. The stack behaves like a LIFO (last-in, first-out) structure but supports rotation. For example, after pushd /tmp, dirs might output /tmp ~, indicating /tmp is now top and ~ (home) is below.

Options allow customization: clearing the stack, verbose listings, or accessing specific stack positions. Note that the stack persists across sessions if Bash history is saved, but is shell-specific.

CAVEATS

Bash built-in only; not available in other shells like zsh or dash. Stack size limited by dirs -l output and Bash configuration (default ~5000 entries). Relative paths change with cd. No effect on actual filesystem.

EXAMPLES

dirs # Default: ~/src /tmp
dirs -v # Verbose: /home/user/src /tmp
dirs +2 # Third from top
dirs -c # Clear stack

STACK MANAGEMENT

Use pushd +N to rotate to Nth position. Stack saved in $DIRSTACK variable. shopt -s dirstackdir enables persistent storage.

HISTORY

Introduced in Bash 1.0 (1989) as part of directory stack features inspired by csh. Evolved with Bash versions; options like -v added in Bash 2.0 (1996). Remains unchanged in modern Bash 5.x.

SEE ALSO

pushd(1), popd(1), cd(1), pwd(1)

Copied to clipboard