How to Use the mkdir Command in Linux?

In Linux, the “mkdir” creates a new directory into the present working directory or at a particular directory. It also defines the permissions for all or specific users during creation.

This post will demonstrate the working/usage of the mkdir command in Linux with the following content:

Let’s start with the working of the “mkdir” command.

How to Use mkdir Command in Linux?

The working of the “mkdir” relies on its syntax. The syntax of the “mkdir” is written below:

Syntax:

$ mkdir [option] dir_name

The above syntax contains the following components:

  • mkdir: Main keyword that represents the “mkdir” command.
  • options: Supported options of the “mkdir” command.
  • dir_name: Denotes the particular created directory name

For more details of the “mkdir” tool, execute its “help” command in the terminal (Ctrl+Alt+T):

$ mkdir --help

Let’s have on to the various examples that describe the practical implementation of the “mkdir” command with its supported options.

Example 1: Creating Directory (Single/Multiple)

The main goal of the “mkdir” without any argument or flag is to create a directory. Let’s explore how it is used to make single/multiple directories.

Creating a Single Directory

In this example, a “NEW” directory is created into the PWD:

$ mkdir NEW

The “New” directory is successfully created, and the “ls -l” command verifies it.

Create Multiple Directories

The user can create more than one directory simultaneously by using the “mkdir” command line tool:

$ mkdir Direc1 Direc2 Direct3 Direc4

All the specified directories have been created into the “Dir1” directory at a time.

Example 2: Create a Directory Tree (Directories Within a Directory)

The “mkdir” command offers the “-p” argument that is beneficial to create the “parent” directory and then subdirectories to it:

Suppose the user creates the “Sample” directory as a parent and then creates the multiple subdirectories into it by using the “mkdir” command:

$ mkdir -pv Sample/Dir1/Dir2/

The “v” option is used to display the above command result into the terminal. It is verified that the parent directory “Sample” has been created having subdirectory “Dir1” and then “Dir2

Example 3: Set Permissions of a Directory

Every file or directory contains three types of permissions, i.e., “r (read)”, “w (write)”, and “x (execute)”. The “mkdir” provides the “-m” option to set these permissions to a specific directory at the time of creation:

In this case, the “rwx” permissions are assigned to the newly created “Hello” directory of all the users of the current system:

$ mkdir -m777 Hello

Again for the verification, use the “ls -l” command:

$ ls -l

It is confirmed that all the users can now read, write and execute thenewly “Hello” directory.

That’s all about the “mkdir” command in Linux.

Conclusion

The “mkdir” Linux command creates new directories in the PWD or a specific directory. This command allows the creation of a parent directory and multiple subdirectories. It is also beneficial to set the permissions of a directory during its creation process. This article has illustrated the goal, working, and complete usage of the “mkdir” command in Linux.