dirs Command in Linux

Introduction to Linux Directory Stack Management

This guide teaches you how to effectively manage your directory navigation history in Linux using the dirs command. Master the directory stack, a powerful feature for systemadmin tasks, allowing you to quickly jump between frequently accessed directories. Learn to use pushd and popd commands alongside dirs to streamline your workflow. This lab provides a hands-on experience covering the syntax and practical applications of the dirs command for displaying and manipulating the directory stack.

This comprehensive tutorial is divided into three key steps: grasping the purpose and syntax of the dirs command, exploring the directory stack, and managing it with pushd and popd. Throughout this learning journey, you'll gain practical experience using various options of the dirs command to display the contents of the directory stack in different formats.

Understanding the Purpose and Syntax of the dirs Command

This section delves into the core functionality of the dirs command in Linux. The dirs command is your go-to tool for viewing the directory stack, a record of the directories you've visited within your current terminal session. This is invaluable for systemadmin tasks requiring frequent directory hopping.

Let's begin. Open your terminal and navigate to the ~/project directory:

cd ~/project

Now, let's invoke the dirs command:

dirs

This will display the current directory stack's content. As you've just opened the terminal, the output is likely:

 ~/project

Without any flags, the dirs command shows you the existing directory stack.

Enhance your usage with these options for the dirs command:

  • -c: Empties the directory stack.
  • -l: Shows the full directory paths in the stack.
  • -p: Prints one directory per line for better readability.
  • -v: Shows the stack with associated line numbers.

Here's an example:

dirs -l

The output displays the full path:

 /home/labex/project

Now, let's add directories to the stack using pushd command:

pushd /tmp
pushd /var/log
pushd /etc

After these commands, your directory stack should resemble this:

 /etc
 /var/log
 /tmp
 /home/labex/project

Execute dirs again to view the updated stack.

Exploring the Directory Stack Using the dirs Command

This section explores how to effectively navigate and manage the directory stack using the dirs command.

First, let's refresh our view of the current directory stack:

dirs

You should see the following:

 /etc
 /var/log
 /tmp
 /home/labex/project

The dirs command shows the stack's contents with the most recently added directory on top.

Let's examine additional dirs options:

dirs -v

This displays the directory stack with line numbers:

 0 /etc
 1 /var/log
 2 /tmp
 3 /home/labex/project

The -v option helps to reference directories within the stack by adding line numbers to the output. An invaluable option for systemadmin.

You can also navigate using the dirs command and cd. For example, change to the directory at index 1 (/var/log) using:

cd +1

This takes you to /var/log.

To return to the previous directory:

cd -

This returns you to /etc.

Practice using the dirs, cd +n, and cd - commands to navigate and manage the directory stack.

Managing the Directory Stack with pushd and popd Commands

This section demonstrates how to use the pushd and popd commands for directory stack management.

The pushd command adds the current directory to the stack's top, then moves you to the new directory specified as its argument. For example:

pushd /tmp

This adds /tmp to the top of the stack and changes the current directory to /tmp.

Use popd to remove the top directory from the stack, and move the current directory to the new top of stack. For example:

popd

This removes the top directory (/tmp) and changes the current directory to what was below /tmp in the stack.

Let's try more examples:

pushd /etc
pushd /var/log
pushd /home/labex/project
dirs -v

This adds /etc, /var/log, and /home/labex/project to the stack, then shows it with line numbers:

 0 /home/labex/project
 1 /var/log
 2 /etc
 3 /home/labex/project

Now, use popd to navigate back:

popd
popd
popd
dirs -v

This removes the top three directories, and the output of dirs -v should now show:

 0 /home/labex/project

pushd and popd are an efficient way to navigate and manage your directory stack. Practice with these commands to enhance familiarity.

Summary

In this lab, you've learned about the purpose and syntax of the dirs command in Linux, a valuable tool for any systemadmin for viewing the directory stack, a dynamic record of directories visited during a shell session. You examined options like -l for full paths, -p for single-line output, and -v for numbered lists. Furthermore, you have learned how to use the pushd and popd commands, which enable you to manipulate directory stack.

400+ Linux Commands