Introduction
In this lab, you will explore the Linux dir command, which is used to list the contents of a directory. You will learn how to understand the basic usage of the dir command, explore various directory listing options, and manage directories using the dir command. The lab covers practical examples to help you effectively utilize the dir command in your Linux environment.
The lab starts by introducing the dir command and demonstrating its basic usage. You will then dive deeper into the various directory listing options, such as displaying long-format listings, showing hidden files, and sorting the output. Finally, you will learn how to manage directories using the dir command, including navigating between directories and performing common file management tasks.
Understand the dir Command
In this step, we will explore the basic usage of the dir command in Linux. The dir command is used to list the contents of a directory, similar to the ls command. However, dir provides some additional options that can be useful in certain scenarios.
Let's start by running the basic dir command:
dir
Example output:
Desktop Documents Downloads Music Pictures Public Templates Videos
As you can see, the dir command lists the contents of the current directory, which is ~/project in our case.
You can also use the dir command to list the contents of a specific directory:
dir ~/Documents
Example output:
file1.txt file2.txt report.pdf
The dir command supports various options that allow you to customize the directory listing. Some of the commonly used options are:
-l: Display the long-format listing, which includes file permissions, ownership, size, and modification time.-a: Show all files, including hidden files (files starting with a dot).-r: Reverse the order of the listing.-t: Sort the listing by modification time, with the most recent files first.
For example, to list the contents of the current directory in long format, you can use:
dir -l
Example output:
total 16
drwxr-xr-x 2 labex labex 4096 Apr 17 12:34 Desktop
drwxr-xr-x 2 labex labex 4096 Apr 17 12:34 Documents
drwxr-xr-x 2 labex labex 4096 Apr 17 12:34 Downloads
drwxr-xr-x 2 labex labex 4096 Apr 17 12:34 Music
drwxr-xr-x 2 labex labex 4096 Apr 17 12:34 Pictures
drwxr-xr-x 2 labex labex 4096 Apr 17 12:34 Public
drwxr-xr-x 2 labex labex 4096 Apr 17 12:34 Templates
drwxr-xr-x 2 labex labex 4096 Apr 17 12:34 Videos
The dir command is a useful tool for quickly viewing the contents of a directory and can be combined with other commands to perform more advanced file management tasks.
Explore Directory Listing Options
In this step, we will dive deeper into the various options available with the dir command to customize the directory listing.
Let's start by listing the contents of the current directory in long format:
dir -l
Example output:
total 16
drwxr-xr-x 2 labex labex 4096 Apr 17 12:34 Desktop
drwxr-xr-x 2 labex labex 4096 Apr 17 12:34 Documents
drwxr-xr-x 2 labex labex 4096 Apr 17 12:34 Downloads
drwxr-xr-x 2 labex labex 4096 Apr 17 12:34 Music
drwxr-xr-x 2 labex labex 4096 Apr 17 12:34 Pictures
drwxr-xr-x 2 labex labex 4096 Apr 17 12:34 Public
drwxr-xr-x 2 labex labex 4096 Apr 17 12:34 Templates
drwxr-xr-x 2 labex labex 4096 Apr 17 12:34 Videos
The -l option displays the long-format listing, which includes file permissions, ownership, size, and modification time.
You can also list all files, including hidden files (files starting with a dot), by using the -a option:
dir -a
Example output:
. .. .config .local Desktop Documents Downloads Music Pictures Public Templates Videos
To reverse the order of the listing, you can use the -r option:
dir -r
Example output:
Videos Templates Public Pictures Music Downloads Documents Desktop .. .
Finally, you can sort the listing by modification time, with the most recent files first, using the -t option:
dir -t
Example output:
Desktop Documents Downloads Music Pictures Public Templates Videos
You can also combine multiple options to achieve the desired output. For example, to list all files in long format, sorted by modification time, you can use:
dir -alt
Example output:
total 16
drwxr-xr-x 2 labex labex 4096 Apr 17 12:34 Desktop
drwxr-xr-x 2 labex labex 4096 Apr 17 12:34 Documents
drwxr-xr-x 2 labex labex 4096 Apr 17 12:34 Downloads
drwxr-xr-x 2 labex labex 4096 Apr 17 12:34 Music
drwxr-xr-x 2 labex labex 4096 Apr 17 12:34 Pictures
drwxr-xr-x 2 labex labex 4096 Apr 17 12:34 Public
drwxr-xr-x 2 labex labex 4096 Apr 17 12:34 Templates
drwxr-xr-x 2 labex labex 4096 Apr 17 12:34 Videos
Exploring these options will help you customize the directory listing to suit your specific needs and preferences.
Manage Directories with dir Command
In this final step, we will explore how to use the dir command to manage directories.
First, let's create a new directory:
dir -p ~/project/new_directory
Example output:
~/project/new_directory
The -p option ensures that the parent directories are created if they don't already exist.
You can also use the dir command to remove directories:
dir -d ~/project/new_directory
Example output:
~/project/new_directory
The -d option tells dir to remove the specified directory.
To remove a directory and its contents recursively, you can use the following command:
dir -rf ~/project/new_directory
Example output:
~/project/new_directory
The -r option makes the removal recursive, and the -f option forces the removal without prompting for confirmation.
Finally, you can use the dir command to change the current working directory:
dir ~/Documents
Example output:
~/Documents
This changes the current working directory to ~/Documents.
By combining the various options of the dir command, you can effectively manage directories and navigate the file system.
Summary
In this lab, you first learned about the basic usage of the dir command in Linux, which is used to list the contents of a directory. You explored how to use the dir command to list the contents of the current directory as well as a specific directory. Additionally, you discovered various options that can be used with the dir command, such as -l to display the long-format listing, -a to show all files including hidden files, -r to reverse the order of the listing, and -t to sort the listing by modification time. These options allow you to customize the directory listing to suit your needs.
You then delved deeper into the various directory listing options provided by the dir command, learning how to manage directories and perform more advanced file management tasks. The dir command proved to be a versatile tool for quickly viewing and manipulating the contents of directories in your Linux system.



