Linux mdir Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn about the Linux mdir command, which is used to create and manage directories in the Linux operating system. The lab covers the basics of the mdir command, including how to create multiple directories at once, as well as explore advanced options such as creating parent directories, setting permissions, and displaying verbose output. The lab provides practical examples to help you understand and apply the mdir command effectively in your Linux environment.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/BasicFileOperationsGroup -.-> linux/mv("`File Moving/Renaming`") linux/BasicFileOperationsGroup -.-> linux/chmod("`Permission Modifying`") subgraph Lab Skills linux/cd -.-> lab-422797{{"`Linux mdir Command with Practical Examples`"}} linux/mkdir -.-> lab-422797{{"`Linux mdir Command with Practical Examples`"}} linux/ls -.-> lab-422797{{"`Linux mdir Command with Practical Examples`"}} linux/mv -.-> lab-422797{{"`Linux mdir Command with Practical Examples`"}} linux/chmod -.-> lab-422797{{"`Linux mdir Command with Practical Examples`"}} end

Understand the mdir Command

In this step, you will learn about the mdir command, which is used to create and manage directories in the Linux operating system.

The mdir command is a shell built-in command that allows you to create multiple directories at once. It is a convenient way to create a directory structure without having to use the mkdir command repeatedly.

To create a new directory using the mdir command, you can use the following syntax:

mdir directory1 directory2 directory3

This will create three new directories: directory1, directory2, and directory3.

Example output:

$ mdir test1 test2 test3
$ ls
test1  test2  test3

The mdir command also supports several options that allow you to customize its behavior. Some of the commonly used options include:

  • -p or --parents: This option allows you to create parent directories as needed. For example, mdir -p a/b/c will create the directory structure a/b/c even if the parent directories (a and b) do not exist.
  • -v or --verbose: This option displays a message for each directory created.
  • -m or --mode=MODE: This option sets the permission mode for the created directories.

Example usage:

$ mdir -p a/b/c
$ ls -l
drwxr-xr-x 3 labex labex 4096 Apr 12 12:34 a

In this example, the -p option was used to create the directory structure a/b/c, and the default permissions were set (rwxr-xr-x).

Create and Manage Directories with mdir

In this step, you will learn how to create and manage directories using the mdir command.

First, let's create a new directory structure using the mdir command:

mdir -p projects/app1 projects/app2 projects/app3

This will create the following directory structure:

$ tree projects
projects
├── app1
├── app2
└── app3

The -p option ensures that the parent directories (projects) are created if they don't already exist.

Now, let's explore some additional options for managing directories with mdir:

Removing directories

To remove directories, you can use the rmdir command. For example, to remove the projects/app2 directory:

rmdir projects/app2

Renaming directories

To rename a directory, you can use the mv (move) command. For example, to rename projects/app1 to projects/frontend:

mv projects/app1 projects/frontend

Changing directory permissions

You can use the chmod command to change the permissions of a directory. For example, to make the projects/app3 directory writable for the group:

chmod g+w projects/app3

Example output:

$ ls -ld projects/app3
drwxr-xr-x 2 labex labex 4096 Apr 12 12:34 projects/app3
$ chmod g+w projects/app3
$ ls -ld projects/app3
drwxrwxr-x 2 labex labex 4096 Apr 12 12:34 projects/app3

Explore Advanced mdir Command Options

In this final step, you will learn about some advanced options available with the mdir command.

Create Directories with Specific Permissions

You can use the -m or --mode option to set the permissions for the directories created by mdir. For example, to create directories with rwxr-xr-x permissions:

mdir -m 755 projects/app4 projects/app5

Example output:

$ ls -ld projects/app4 projects/app5
drwxr-xr-x 2 labex labex 4096 Apr 12 12:34 projects/app4
drwxr-xr-x 2 labex labex 4096 Apr 12 12:34 projects/app5

Create Directories with Timestamps

You can use the --date option to set the creation timestamp for the directories. This can be useful for organizing your files and directories by date. For example, to create directories with the current date:

mdir --date=now projects/2023-04-12

Example output:

$ ls -ld projects/2023-04-12
drwxr-xr-x 2 labex labex 4096 Apr 12 12:34 projects/2023-04-12

Create Directories Recursively

The -p or --parents option allows you to create parent directories as needed. This can be useful when creating a complex directory structure. For example:

mdir -p projects/dev/frontend projects/dev/backend projects/dev/database

This will create the entire directory structure, even if the parent directories (projects/dev) don't exist.

Example output:

$ tree projects
projects
└── dev
    ├── backend
    ├── database
    └── frontend

Summary

In this lab, you learned about the mdir command, which is a shell built-in command used to create and manage directories in the Linux operating system. You understood the basic syntax of the mdir command and explored various options such as -p to create parent directories, -v for verbose output, and -m to set permissions. Additionally, you learned how to create and manage directories using the mdir command, including creating directory structures and exploring advanced options like renaming and removing directories.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like