The dirs
command is a built-in shell command in Linux that displays the directory stack, which is a list of directories that have been visited by the user. The directory stack is used to keep track of the directories that the user has visited and can be used to quickly move back to previously visited directories.
Overview
The dirs
command can be used to display the directory stack in the current shell session. By default, the dirs
command displays the directory stack in a vertical list, with the most recent directory at the top of the list. Each directory in the list is displayed with its index number and the full path to the directory.
The directory stack can be manipulated using other shell commands such as pushd
and popd
. The pushd
command adds a directory to the directory stack and changes the current directory to the specified directory. The popd
command removes a directory from the directory stack and changes the current directory to the next directory in the stack.
Here are some examples of how to use the dirs
command:
- To display the directory stack in a vertical list:
$ dirs
/home/user/documents
/home/user/downloads
/home/user
- To display the directory stack in a horizontal list:
$ dirs -l
/home/user/documents /home/user/downloads /home/user
- To display the directory stack with index numbers:
$ dirs -v
0 /home/user/documents
1 /home/user/downloads
2 /home/user
- To change the current directory to a directory in the stack:
$ cd -2
/home/user
Options
The dirs
command has the following options:
Option | Description |
---|---|
-c |
Clears the directory stack. |
-l |
Displays the directory stack in a horizontal list. |
-p |
Displays the directory stack in a format that can be used as input to the cd command. |
-v |
Displays the directory stack with index numbers. |
Troubleshooting tips
- If the
dirs
command does not display any directories, it means that the directory stack is empty. The user must visit a directory using thecd
command before the directory is added to the stack. - If the
dirs
command displays an error message, it could be due to a syntax error in the command. Check the command syntax and try again.
Notes
- The directory stack is specific to each shell session. If a new shell session is opened, the directory stack will be empty.
- The directory stack can hold a maximum of 20 directories by default. This limit can be changed by modifying the
DIRSTACKSIZE
environment variable.