Linux dirs Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the dirs command in Linux to manage and explore the directory stack, which is a list of directories that you have navigated to during your current shell session. You will also learn how to use the pushd and popd commands to manipulate the directory stack. The lab covers the purpose and syntax of the dirs command, as well as practical examples of how to use it to display and manage the directory stack.

The lab consists of three main steps: understanding the purpose and syntax of the dirs command, exploring the directory stack using the dirs command, and managing the directory stack with the pushd and popd commands. Throughout the lab, you will practice using the dirs command with various options to display the contents of the directory stack in different formats.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/FileandDirectoryManagementGroup -.-> linux/pwd("`Directory Displaying`") subgraph Lab Skills linux/cd -.-> lab-422641{{"`Linux dirs Command with Practical Examples`"}} linux/pwd -.-> lab-422641{{"`Linux dirs Command with Practical Examples`"}} end

Understand the Purpose and Syntax of the dirs Command

In this step, you will learn about the purpose and syntax of the dirs command in Linux. The dirs command is used to display the contents of the directory stack, which is a list of directories that you have navigated to during your current shell session.

To begin, open a terminal and navigate to the ~/project directory:

cd ~/project

Now, let's explore the dirs command:

dirs

This will display the current contents of the directory stack. Since you just opened the terminal, the output should be:

 ~/project

The dirs command without any options simply displays the current directory stack.

You can also use the dirs command with the following options:

  • -c: Clears the directory stack.
  • -l: Displays the full paths in the directory stack.
  • -p: Displays one directory per line.
  • -v: Displays the directory stack with line numbers.

For example, try the following:

dirs -l

This will display the full path of the current directory in the stack:

 /home/labex/project

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

pushd /tmp
pushd /var/log
pushd /etc

After running these commands, the directory stack will look like this:

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

You can see the updated stack by running dirs again.

Explore the Directory Stack Using the dirs Command

In this step, you will learn how to explore and manipulate the directory stack using the dirs command.

First, let's review the current state of the directory stack:

dirs

You should see the following output:

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

The dirs command displays the current contents of the directory stack, with the most recently added directory at the top.

Now, let's explore some additional options for the dirs command:

dirs -v

This will display the directory stack with line numbers:

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

The -v option adds line numbers to the output, making it easier to reference specific directories in the stack.

You can also use the dirs command to navigate the directory stack. For example, to change to the directory at index 1 (in this case, /var/log), you can use the following command:

cd +1

This will change the current directory to /var/log.

To navigate back to the previous directory, you can use:

cd -

This will change the current directory to the previous one in the stack, which is /etc.

Try experimenting with the dirs command and the cd +n and cd - commands to navigate the directory stack.

Manage the Directory Stack with pushd and popd Commands

In this step, you will learn how to manage the directory stack using the pushd and popd commands.

The pushd command adds the current directory to the top of the directory stack and then changes the current directory to the new directory specified as an argument. For example:

pushd /tmp

This will add /tmp to the top of the directory stack and change the current directory to /tmp.

You can then use the popd command to remove the top directory from the stack and change the current directory to the new top directory. For example:

popd

This will remove the top directory from the stack (which was /tmp) and change the current directory to the new top directory.

Let's try some more examples:

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

This will add /etc, /var/log, and /home/labex/project to the directory stack, and then display the stack with line numbers:

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

Now, let's use popd to navigate back through the stack:

popd
popd
popd
dirs -v

This will remove the top three directories from the stack, and the output of dirs -v should now be:

 0 /home/labex/project

The pushd and popd commands provide a convenient way to navigate through directories and manage the directory stack. Experiment with these commands to become more familiar with their usage.

Summary

In this lab, you learned about the purpose and syntax of the dirs command in Linux, which is used to display the contents of the directory stack - a list of directories you have navigated to during your current shell session. You explored different options for the dirs command, such as -l to display full paths, -p to show one directory per line, and -v to display the stack with line numbers. Additionally, you learned how to manage the directory stack using the pushd and popd commands, which allow you to add and remove directories from the stack, respectively.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like