Linux mkdir Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to create directories and manage permissions using the Linux mkdir command. The lab covers creating single or multiple directories, creating nested directories with the -p option, and managing permissions with the mkdir command. The content covers practical examples and step-by-step instructions to help you become proficient in basic file and directory operations in a 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/chmod("`Permission Modifying`") subgraph Lab Skills linux/cd -.-> lab-422804{{"`Linux mkdir Command with Practical Examples`"}} linux/mkdir -.-> lab-422804{{"`Linux mkdir Command with Practical Examples`"}} linux/ls -.-> lab-422804{{"`Linux mkdir Command with Practical Examples`"}} linux/chmod -.-> lab-422804{{"`Linux mkdir Command with Practical Examples`"}} end

Create Directories with mkdir Command

In this step, you will learn how to create directories using the mkdir command in Linux.

The mkdir command is used to create new directories. You can create a single directory or multiple directories at once.

To create a new directory, use the following syntax:

mkdir directory_name

Example:

$ mkdir mydir
$ ls
mydir

In the example above, we created a new directory named mydir using the mkdir command. You can verify the directory was created by running the ls command.

You can also create multiple directories at once by providing multiple directory names separated by spaces:

mkdir dir1 dir2 dir3

Example output:

$ mkdir dir1 dir2 dir3
$ ls
dir1  dir2  dir3  mydir

Now, let's create a directory structure with multiple levels:

mkdir -p parent/child/grandchild

The -p option in the mkdir command allows you to create the entire directory structure, including any necessary parent directories, in a single command.

Example output:

$ mkdir -p parent/child/grandchild
$ ls -R
.:
child  parent

./parent:
child

./parent/child:
grandchild

As you can see, the mkdir -p command created the parent, child, and grandchild directories in a single step.

Create Nested Directories with mkdir -p

In this step, you will learn how to create nested directories using the mkdir -p command in Linux.

The mkdir -p command allows you to create a directory structure with multiple levels in a single command. This is useful when you need to create a directory and its parent directories at the same time.

Let's create a nested directory structure:

mkdir -p projects/web-app/src/components

Example output:

$ mkdir -p projects/web-app/src/components
$ ls -R
projects

./projects:
web-app

./projects/web-app:
src

./projects/web-app/src:
components

As you can see, the mkdir -p command created the entire directory structure, including the projects, web-app, src, and components directories.

Now, let's create another nested directory structure:

mkdir -p documents/reports/2023/q1

Example output:

$ mkdir -p documents/reports/2023/q1
$ ls -R
documents  projects

./documents:
reports

./documents/reports:
2023

./documents/reports/2023:
q1

./projects:
web-app

The mkdir -p command allows you to create the entire directory structure, including the documents, reports, 2023, and q1 directories, in a single step.

Permissions Management with mkdir

In this step, you will learn how to manage permissions when creating directories using the mkdir command in Linux.

By default, when you create a new directory using mkdir, the directory inherits the permissions of the parent directory. However, you can also specify the permissions explicitly when creating the directory.

To create a new directory with specific permissions, you can use the -m option followed by the permission mode:

mkdir -m 755 my_dir

In the example above, we create a new directory named my_dir with permissions set to 755 (read, write, and execute for the owner; read and execute for the group and others).

You can also use symbolic permissions instead of numeric modes:

mkdir -m u=rwx,g=rx,o=rx my_dir

This command creates the my_dir directory with the same permissions as the previous example, but using symbolic notation.

Let's create a directory with different permissions:

mkdir -m 700 secret_dir

This creates a new directory named secret_dir with permissions set to 700 (read, write, and execute for the owner; no access for the group and others).

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

$ ls -l
total 8
drwxr-xr-x 2 labex labex 4096 Apr 12 12:34 my_dir
drwx------ 2 labex labex 4096 Apr 12 12:35 secret_dir

As you can see, the my_dir directory has permissions 755, while the secret_dir directory has permissions 700.

Summary

In this lab, you learned how to create directories using the mkdir command in Linux. You can create a single directory or multiple directories at once, and you can also create nested directories using the -p option. Additionally, you learned how to manage permissions when creating directories. The key takeaways from this lab are the various use cases of the mkdir command, including creating directories, creating nested directories, and managing permissions.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like