dirs

2024-03-23

Understanding 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.

Basic Usage: Displaying the Directory Stack

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 Stack

The 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

Advanced dirs Options

dirs offers some additional options for more fine-grained control:

pushd ~/projects/projectA/src
pushd ~/projects/projectB
dirs -v
dirs -v
dirs +2 # Changes to the second directory in the stack
pwd
dirs -c
dirs # The stack will be empty

Integrating dirs into your Workflow

By 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.