How to use dirs command in Linux.

How To Use Dirs Command In Linux.

In the world of command-line interfaces, efficient navigation and management of directories are essential skills. One such tool that can significantly streamline directory handling in Unix-based systems is the ‘dirs’ command. The ‘dirs’ command is often overlooked, but it can be a powerful ally for users looking to enhance their command-line productivity. This guide aims to provide a comprehensive understanding of the ‘dirs’ command, along with numerous examples to demonstrate its functionality.

The Basics of ‘dirs’

The ‘dirs’ command is used to display and manipulate the directory stack, which is a history of directories that you have visited during your current session. The directory stack, often referred to as the “dir stack,” allows you to easily switch between directories and manage your navigation history.


Basic ‘dirs’ Syntax:

dirs [OPTION]

Common Options

  1. -c or –clear: Clears the directory stack entirely.

  2. -l or –long: Displays the directory stack with one entry per line.

  3. -p or –print: Prints the directory stack, separated by spaces.

  4. -N or –no-dupes: Prevents adding duplicate directories to the stack.

  5. -v or –verbose: Prints the directory stack with indices.


  1. Display the Directory Stack:

  1. Display the Directory Stack with Indices:

dirs -v
  1. Display the Directory Stack with One Entry per Line:

dirs -l
  1. Clear the Directory Stack:

dirs -c
  1. Prevent Duplicate Entries in the Directory Stack:

dirs -N

Navigating the Directory Stack

The ‘dirs’ command is particularly useful when you want to move between directories in your history. You can use ‘cd’ in conjunction with ‘dirs’ to navigate through the directory stack.


Navigation Example: Suppose you have the following directory stack:

1 /home/user/documents
2 /var/www
3 /etc

To move to ‘/var/www’, you can use the following commands:

cd -2

This moves you to ‘/var/www’ because ‘-2’ corresponds to its position in the stack.

Manipulating the Directory Stack

You can also use ‘dirs’ to manipulate the directory stack, such as adding or removing entries.


Adding Directories to the Stack: To add a directory to the stack, simply use ‘pushd’ followed by the directory path. For example:

pushd /tmp

This will add ‘/tmp’ to the directory stack.


Removing a Directory from the Stack: You can remove directories from the stack using ‘popd.’ For instance:


This will remove the top directory from the stack.

Using the Directory Stack with History

You can combine the ‘dirs’ command with command history for more efficient navigation. For example, if you want to revisit a directory from your command history, you can use the following command:

cd ~-3

This command takes you to the directory you were in three commands ago.

  1. Specifying Directory Stack Size:

By default, the directory stack can hold a limited number of entries. You can specify the maximum size of the directory stack using the ‘dirs -p’ command, like this:

shellCopy code


dirs -p -c 50

This sets the directory stack size to 50 entries. Adjust the number to suit your needs.

  1. Rotating the Directory Stack:

You can rotate the directory stack to bring specific directories to the top. For example, if you want to make ‘/var/www’ the top entry in the stack, use this command:

dirs -p -c 50

This shifts ‘/var/www’ to the top of the stack.

  1. Using ‘dirs’ in Shell Scripts:

You can incorporate the ‘dirs’ command into shell scripts to automate directory management. For example, you can create a script to navigate to a directory, perform tasks, and then return to the previous directory. Here’s a simple script:

start_dir=$(dirs -l)
cd /path/to/some/directory
# Perform tasks in the specified directory
cd $start_dir
  1. Quick Directory Switching:

If you often switch between two specific directories, you can use ‘dirs’ and ‘cd’ in combination with aliases. For instance:

alias godocs='cd -2'
alias projects='cd -3'

Now, you can quickly switch to the second and third directories in your stack with ‘godocs’ and ‘projects’ aliases.

  1. Combining ‘dirs’ with ‘ls’:

To list the contents of a directory from your stack without actually switching to it, use a combination of ‘dirs’ and ‘ls’:

ls $(dirs +2)

This lists the contents of the directory at position +2 in your stack.

  1. Automatically Adding to the Directory Stack:

To automatically add a directory to the stack every time you navigate, use the PROMPT_COMMAND environment variable. Add this line to your shell configuration file (e.g., .bashrc or .zshrc):

export PROMPT_COMMAND='dirs -p'

This will keep your directory stack up to date every time you run a command.



The ‘dirs’ command is a valuable tool for managing directory navigation in Unix-based systems. With its various options and capabilities, it empowers users to work more efficiently in the command-line environment. By mastering ‘dirs’ and its associated commands, you can streamline your workflow and improve your productivity when working with directories.