dirs - Linux


The dirs command in Linux displays a list of directories stored in the directory stack. This stack is a list that keeps track of the directories the user has visited during a terminal session, allowing for easy navigation back and forth. Primarily used in shell scripts and terminal sessions for efficient directory management, dirs assists in displaying, controlling, and manipulating the directories in the stack.


The basic syntax of the dirs command is:

dirs [options] [+N] [-N]
  • +N: Displays the N-th directory (counting from the left, starting at zero) in the stack.
  • -N: Displays the N-th directory (counting from the right).

These options can be used to view specific directories within the stack based on their positional index.


  • -c: Clears the directory stack by removing all entries except the current directory.
  • -l: Produces output in a long format, i.e., no tilde (~) for the user’s home directory.
  • -p: Prints the directory stack with one entry per line, making it more readable especially when directories have spaces.
  • -v: Verbose mode. Similar to -p but prefixes each directory with its position in the stack.

Each option modifies the display or behavior of the command to accommodate needs such as clearing the stack or adjusting the display format.


  1. Displaying the directory stack:

    dirs -v

    This command will list all directories in the stack with their respective indices.

  2. Clearing the directory stack:

    dirs -c

    Resets the stack to have only the current directory.

  3. Showing the third directory from the left:

    dirs +2

    Since indexing starts at 0, +2 refers to the third directory.

  4. Using long format to avoid tildes:

    dirs -l

    Useful for getting the full path for copying or script usage.

Common Issues

  • Ambiguity in Stack Indexing: Users may misunderstand the zero-based indexing (where the first index is 0). Always account for this in scripts or when using flags like +N or -N.
  • Overwriting the Stack: Frequent use of pushd or popd without monitoring can lead to losing track of directories. Regularly outputting the stack with dirs -v can prevent confusion.


dirs can be effectively combined with pushd and popd for advanced directory traversal tasks:

# Navigate to different directories and track them
pushd /var/log
pushd /etc
dirs -v  # View the stacked directories

# Pop the top directory and check remaining
dirs -v

This approach helps in scripting scenarios where operations need to be performed in different directories sequentially.

  • pushd: Adds directories to the stack and changes the current directory to it.
  • popd: Removes entries from the directory stack and changes the directory to the one that was most recently stored.

For more detail and further reading on directory stack commands, consult the GNU Bash documentation: GNU Bash Reference.