2024-03-23
dirs
The dirs
command, short for “directories,” displays the current working directory stack. This stack is a list of directories you’ve visited using pushd
and popd
(explained below). Understanding this stack is important for quickly navigating between different project directories or configurations.
The simplest use of dirs
is to simply list the directories in your stack. Execute the following in your terminal:
dirs
This will output a space-separated list of directories, with the currently active directory appearing first. If you haven’t used pushd
or popd
yet, you’ll likely only see your current working directory.
pushd
and popd
: Manipulating the Directory StackThe dirs
command works in tandem with pushd
(push directory) and popd
(pop directory) to effectively manage your directory history.
pushd <directory>
: This command adds the specified directory to the top of the directory stack and changes your current working directory to that directory.
mkdir -p ~/projects/projectA/src ~/projects/projectB
pushd ~/projects/projectA/src
pwd # Verify the current directory
dirs # Show the directory stack
pushd ~/projects/projectB
pwd
dirs
popd
: This command removes the top directory from the stack and changes your current working directory to the next directory in the stack.
popd # Go back to ~/projects/projectA/src
pwd
dirs
popd # Go back to your home directory (assuming that's where you started)
pwd
dirs
dirs
Optionsdirs
offers some additional options for more fine-grained control:
dirs -v
(verbose): This option displays the directory stack with numbered indices, making it easier to refer to specific entries.pushd ~/projects/projectA/src
pushd ~/projects/projectB
dirs -v
dirs +<number>
: This allows accessing a specific directory in the stack by its index (as shown with dirs -v
). For instance, dirs +2
would change the current directory to the second directory in the stack.dirs -v
dirs +2 # Changes to the second directory in the stack
pwd
dirs -c
(clear): This clears the entire directory stack. Use this with caution, as it will discard your entire directory history managed by pushd
and popd
.dirs -c
dirs # The stack will be empty
dirs
into your WorkflowBy incorporating pushd
, popd
, and dirs
into your workflow, you can streamline your navigation between different projects and directories, saving time and reducing errors. The numbered indices provided by dirs -v
are particularly helpful when dealing with a complex directory structure or many simultaneously active projects. Mastering these commands will improve your Linux command-line proficiency.