![UNIX Tutorial](/unix/images/unix-mini-logo.jpg)
- Unix Commands Reference
- Unix Commands - Home
dirs Command in Linux
The Linux dirs command displays the list of directories currently stored in the directory stack. The directory stack is managed by the companion commands pushd and popd. It is a handy tool to navigate and manage directories on Linux.
Note that the directory stack is a feature of the shell session and persists as long as the shell session is active.
Table of Contents
- Syntax for the dirs Command
- Options for the dirs Command
- Understanding the pushd and popd Commands
- Using dirs Command in Linux
Syntax for the dirs Command
The general syntax for the Linux dirs command is as follows −
dirs [options] [+N | -N]
The [options] field in the syntax is used to specify options to modify the dirs command output. While +N and -N fields display the Nth directory in the stack from left, and right respectively.
Options for the dirs Command
The options used with the dirs command are given in the table below −
Options | Description |
---|---|
-c | It clears the directory stack by deleting all entries |
-l | It replaces the tilde ~ with the absolute path |
-p | It displays the directory stack with one entry per line |
-v | It displays the directory stack with one entry per line along with stack position number |
+N | It displays the Nth directory from the left starting with 0 |
-N | It displays the Nth directory from the right starting with 0 |
Understanding the pushd and popd Commands
The dirs command is incomplete without the companion commands, pushd, and popd. These commands essentially manage the stack.
The pushd Command
The pushd command adds the specified directory to the stack and switches to it.
Options for the pushd command are given below −
Options | Description |
---|---|
-n | It switches the directory without altering the stack |
+N | It brings the Nth directory to the top by rotating the stack (from left) |
-N | It brings the Nth directory to the top by rotating the stack (from right) |
The popd Command
The popd command removes the specified directory from the stack and switches to the next one.
Options for the popd command are mentioned in the table below −
Options | Description |
---|---|
-n | It switches the directory without altering the stack |
+N | It removes the Nth directory from the stack from the left |
-N | It removes the Nth directory from the stack from the right |
It can be noticed that +N and -N have different functionality if used with pushd and popd.
Using dirs Command in Linux
This section demonstrates the dirs command usage in Linux −
Basic Usage of dirs Command
If the dirs command is executed without any option, it will display the current directory −
![Basic Usage of dirs Command 1](/unix_commands/images/basic_usage_of_dirs_command_1.jpg)
Because there is only one directory in the stack.
Note − If you switch the directory stack using the cd command, the dirs will only show the current directory. The cd command does not push the directory entry to the stack as shown in the following image.
![Basic Usage of dirs Command 2](/unix_commands/images/basic_usage_of_dirs_command_2.jpg)
To create a stack, the pushd command is used. Let’s add a directory to the stack as a first element −
pushd directory2
![Basic Usage of dirs Command 3](/unix_commands/images/basic_usage_of_dirs_command_3.jpg)
This command switches the current directory to the ~/directory1/directory2 and adds it to the top of the stack.
To view the stack, use the dirs command −
dirs
![Basic Usage of dirs Command 4](/unix_commands/images/basic_usage_of_dirs_command_4.jpg)
In the same way, more directories can be pushed to the stack.
To remove the top directory from the stack, the popd command is used. It switches to the next directory it removed −
popd
![Basic Usage of dirs Command 5](/unix_commands/images/basic_usage_of_dirs_command_5.jpg)
The above output shows that the directory at the 0th (top) position is removed and switched to the next directory.
Remove Tilde (~) from the Directory Paths
By default, the dirs command displays the tilde (~) with the directory paths.
To display the directories with full path, use the -l option −
dirs -l
![Remove Tilde from the Directory Paths](/unix_commands/images/remove_tilde_from_the_directory_paths.jpg)
Display Stack Entries on Separate Lines
To display the stack entries on a separate line, the -p option is used −
dirs -p
![Display Stack Entries on Separate Lines](/unix_commands/images/display_stack_entries_on_separate_lines.jpg)
Display Stack Entry Numbers
To display the directory stack entries with numbers, use the -v option. It displays each entry on a separate line with a number −
dirs -v
![Display Stack Entry Numbers](/unix_commands/images/display_stack_entry_numbers.jpg)
Clearing the Directory Stack
To remove all the stack entries use, the -c option −
dirs –c
Display a Specific Directory
To display specific directory paths +N and -N arguments are used. The N signifies the number. The stack count starts from zero. For example, to display the third entry of the directory stack from the left, +2 is used.
dirs +2
To display the first stack entry from the right use +1.
dirs -1
![Display a Specific Directory](/unix_commands/images/display_a_specific_directory.jpg)
Note that the first entry either from left or right is specified by 0.
Managing Stack Entries
To manage the stack entries the pushd and popd commands are used with +N and -N options. The +N and -N have different roles in the pushd and popd commands.
The pushd command with +N or -N alters the directory position in the stack.
For instance, to bring the third entry to the top of the stack use −
pushd +2
![Managing Stack Entries 1](/unix_commands/images/managing_stack_entries_1.jpg)
On the other hand, the "+N" and "-N" options with popd are used to remove the specific directory in a stack. For example, to remove a specific entry from the right side of the stack, use popd with +N.
popd +2
![Managing Stack Entries 2](/unix_commands/images/managing_stack_entries_2.jpg)
To perform the same operation from the right, change the "+" sign with "−".
By default, the pushd command adds the directory to the stack and switches to it. But to add a directory to the stack without switching the -n option is used.
For example, the following command adds the entry to the stack but does not switch to it.
pushd -n ~/directory1/directory2
![Managing Stack Entries 3](/unix_commands/images/managing_stack_entries_3.jpg)
To remove the directory without switching to the next directory -n is used with popd −
popd -n 2
![Managing Stack Entries 4](/unix_commands/images/managing_stack_entries_4.jpg)
Conclusion
The dirs command in Linux lists the directory stack that is currently viewed. The pushd command adds a directory to the stack and switches to it, while popd removes the top directory from the stack and switches to the next one.
The dirs and its associated commands are handy tools for directory management in Linux. This tutorial covered the syntax of the dirs command, its options, and usage. We also explained the concept of directory stack management using pushd and popd.