Linux mdel Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the mdel command in Linux to create and manage multiple directories efficiently. The mdel command allows you to create multiple directories at once, which can save time when setting up a complex directory structure.

You will start by understanding the purpose and syntax of the mdel command, including its available options and common use cases. Then, you will practice creating and managing multiple directories using the mdel command, exploring advanced scenarios such as creating intermediate directories as needed.

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/mkdir("`Directory Creating`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/BasicFileOperationsGroup -.-> linux/rm("`File Removing`") linux/BasicFileOperationsGroup -.-> linux/touch("`File Creating/Updating`") linux/BasicFileOperationsGroup -.-> linux/chmod("`Permission Modifying`") subgraph Lab Skills linux/mkdir -.-> lab-422795{{"`Linux mdel Command with Practical Examples`"}} linux/ls -.-> lab-422795{{"`Linux mdel Command with Practical Examples`"}} linux/rm -.-> lab-422795{{"`Linux mdel Command with Practical Examples`"}} linux/touch -.-> lab-422795{{"`Linux mdel Command with Practical Examples`"}} linux/chmod -.-> lab-422795{{"`Linux mdel Command with Practical Examples`"}} end

Understand the Purpose and Syntax of the mdel Command

In this step, you will learn about the purpose and syntax of the mdel command in Linux. The mdel command is used to create multiple directories at once, which can be a time-saving feature when you need to create a directory structure with several levels.

To understand the basic syntax of the mdel command, run the following command:

man mdel

This will display the manual page for the mdel command, which provides information about its usage and available options.

The basic syntax of the mdel command is as follows:

mdel [options] directory1 directory2 directory3 ...

The mdel command takes one or more directory names as arguments, and creates them all at once. Some common options include:

  • -p: Create intermediate directories as needed. This is useful when creating a directory structure with multiple levels.
  • -v: Verbose mode, which displays the name of each directory as it is created.
  • -m: Set the mode (permissions) of the created directories.

For example, to create three directories named dir1, dir2, and dir3 in the ~/project directory, you can use the following command:

mdel dir1 dir2 dir3

Example output:

~/project/dir1
~/project/dir2
~/project/dir3

In the next step, you will practice creating and managing multiple directories using the mdel command.

Create and Manage Multiple Directories Using mdel

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

First, let's create a few directories in the ~/project directory:

mdel dir1 dir2 dir3

Example output:

~/project/dir1
~/project/dir2
~/project/dir3

As you can see, the mdel command created all three directories at once.

Now, let's create a more complex directory structure using the -p option:

mdel -p dir1/subdir1 dir1/subdir2 dir2/subdir1

Example output:

~/project/dir1
~/project/dir1/subdir1
~/project/dir1/subdir2
~/project/dir2
~/project/dir2/subdir1

The -p option ensures that any necessary intermediate directories are created automatically.

To verify the directories that have been created, you can use the ls command:

ls -l ~/project

Example output:

total 12
drwxrwxr-x 3 labex labex 4096 Apr 12 12:34 dir1
drwxrwxr-x 2 labex labex 4096 Apr 12 12:34 dir2
drwxrwxr-x 3 labex labex 4096 Apr 12 12:34 dir3

You can also use the -v option to see the names of the directories as they are created:

mdel -v dir4 dir5 dir6

Example output:

~/project/dir4
~/project/dir5
~/project/dir6

In the next step, you will explore more advanced options and scenarios of the mdel command.

Explore Advanced Options and Scenarios of the mdel Command

In this final step, you will explore some advanced options and scenarios of the mdel command.

One useful option is the -m flag, which allows you to set the permissions of the created directories. For example, to create a directory with 755 permissions:

mdel -m 755 dir7

Example output:

~/project/dir7

You can verify the permissions using the ls -l command:

ls -ld ~/project/dir7

Example output:

drwxr-xr-x 2 labex labex 4096 Apr 12 12:34 /home/labex/project/dir7

Another advanced scenario is creating directories with spaces in the names. To do this, you can use quotes around the directory names:

mdel "dir 8" "dir 9"

Example output:

~/project/dir 8
~/project/dir 9

You can also use the mdel command to create directories in different locations, not just in the current working directory. To do this, simply provide the full path to the directory you want to create:

mdel /tmp/dir10 /tmp/dir11

Example output:

/tmp/dir10
/tmp/dir11

In this final step, you have learned how to use advanced options and scenarios of the mdel command to create directories with specific permissions and names, as well as in different locations.

Summary

In this lab, you learned about the purpose and syntax of the mdel command in Linux, which is used to create multiple directories at once. You explored the basic syntax of the mdel command and its common options, such as -p to create intermediate directories and -v for verbose mode. You then practiced creating and managing multiple directories using the mdel command, including creating a complex directory structure with the -p option. Overall, the mdel command can be a time-saving feature when you need to create a directory structure with several levels.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like