Skip to content

What is dirs Linux command?

The dirs command in Linux is used to display the list of currently remembered directories in the directory stack. It enables users to navigate through directories efficiently by allowing them to push and pop directories onto and from the stack.

dirs Syntax:

Terminal window
dirs [-c] [+n] [-n]

dirs Options:

OptionDescription
-cClear the directory stack
+nDisplays the nth entry from the directory stack
-nDisplays the nth entry from the directory stack starting from the top (opposite of +n)

Parameters:

ParameterDescription
nA number indicating the nth entry in the directory stack

dirs Command Usage Examples:

Terminal window
dirs

This command prints the current directory stack.

Add a Directory to the Stack

Terminal window
dirs -p /home/user/directory

Adds the directory “/home/user/directory” to the directory stack.

Display Directory Stack with Indexes

Terminal window
dirs -v

Displays the directory stack along with indexes for each entry.

Remove Specific Directory from the Stack

Terminal window
dirs -c 2

Removes the directory at index 2 from the directory stack.

Clear Directory Stack

Terminal window
dirs -c

Clears the entire directory stack.

How do I use dirs in Linux?

To use the dirs command in bash, execute the following command:

Terminal window
dirs

How can I clear the directory stack in Linux?

To clear the directory stack in bash, you can use the following command:

Terminal window
dirs -c

How do I display the directory stack in reverse order in Linux?

To display the directory stack in reverse order in bash, you can use the following command:

Terminal window
dirs -r

How can I limit the number of directories displayed by the dirs command?

To limit the number of directories displayed by the dirs command in bash, you can use the following command:

Terminal window
dirs -l 5

How do I display the directory stack with index numbers in Linux?

To display the directory stack with index numbers in bash, you can use the following command:

Terminal window
dirs -v

How can I navigate to a specific directory in the stack using the dirs command?

To navigate to a specific directory in the stack using the dirs command in bash, you can use the following command:

Terminal window
cd $(dirs +2)

How do I swap the top two directories in the stack using the dirs command?

To swap the top two directories in the stack using the dirs command in bash, you can use the following command:

Terminal window
dirs -s

Applications of the dirs command

  • List recently visited directories
  • Change the directory stack order
  • Navigate between recently visited directories
  • Clear the directory stack