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.

Terminal window
dirs [-c] [+n] [-n]
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)
ParameterDescription
nA number indicating the nth entry in the directory stack
Terminal window
dirs

This command prints the current directory stack.

Terminal window
dirs -p /home/user/directory

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

Terminal window
dirs -v

Displays the directory stack along with indexes for each entry.

Terminal window
dirs -c 2

Removes the directory at index 2 from the directory stack.

Terminal window
dirs -c

Clears the entire directory stack.

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

Terminal window
dirs

How can I clear the directory stack in Linux?

Section titled “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?

Section titled “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?

Section titled “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?

Section titled “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?

Section titled “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?

Section titled “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
  • List recently visited directories
  • Change the directory stack order
  • Navigate between recently visited directories
  • Clear the directory stack