Linux mkdir Command

In this tutorial, we will learn about Linux mkdir Command in Linux and we will also cover different examples related to mkdir command.

Linux mkdir Command

In Linux, mkdir stands for make directory. The Name Suggest the purpose of mkdir command is to create a directory.

Syntax:

mkdir directory_name


In the above syntax, directory_name can be anything or as per your required name what we want to give to the project or any folder to save the files under that directory.

Let us understand about the mkdir command using the following examples:

Example 1.0:

In this example we are creating a directory with a name “mydir” and to create this we use the following command to proceed to create this directory.

mkdir mydir

By running the above command it will create the directory at the current location with the name “mydir“.

Output:

After running the following command we can see in the below output that the directory “mydir” is created inside the /demo location.

mkdir command
mkdir command

Creating Multiple Directories

We heard many times in the interviews and even in the viva many person ask the question that Can we create the Multiple Directories at the same time using the single command?

So we have the answer to this question and we will explain this with an example.

The answer is simple yes we can create the multiple directories at the same time using the single command. To understand this properly we can follow the following example below:

Example 1.2:

In this example we are creating the multiple directories with the name “D1”, “D2”, “D3” using the single command.

Syntax:

mkdir D1 D2 D3

By running the above command we will able to create the three directories at the same time into the current location we want to create.

creating multiple directories in linux
Multiple Directories

In the above output we can see that the Directories D1,D2,D3 are created inside the /demo location.

Read: Linux Directory

Creating Directory from current location to other location

In the above topics and the examples we have learned the command to create a directory and a multiple directories but now we are going to show an example to create a directory from the present location to the other location of our system.

Let us suppose that my current location is /demo and I want to create the directory “EXAMPLE3” at location “/home/shivanshu/examples”

To perform this we only need to add the path with the mkdir command.To see this how we can do follow the below syntax:

mkdir /home/shivanshu/EXAMPLE3

Output:

In the below output we can see our current location is /demo and from there we have created a directory inside /home/shivanshu/examples/ in the below output the following example is also known as the Parent Directory of EXAMPLE3 directory.

Creating Directory from current location to other location
Creating Directory from current location to other location

Creating E4 directory inside E3 directory

Note:

While we proceed further please note that the E3 directory does not exist.

Example:

In this example we are required to create E4 directory inside the E3 directory by using the single command.

  • To perform this first we will create a directory E3 then we will go inside that directory and then after we will create a E4 directory at the E3 location.
  • Let us clear that the E3 is also known as the parent directory of the E4 so we are also require to create a parent directory as well with the mkdir command and to make this we can use the -p option.

Syntax:

mkdir -p E3/E4

In the above syntax the -p option will tell mkdir that if the parent directory does not exist then it first need to create that so, this command first creates E3 directory at the current location and the E4 directory inside E3 directory.

Output:

In the below output we can see that the E4 directory is created inside the E3 directory using the single command.

Linux Parent directory

Tasks for Practice

  • What will be the output of command mkdir t1, however t1 directory already exists at current location.
  • What will be the output of command mkdir /t1, however t1 directory already exists at current location and your current location is /demo.
  • What will be the output of command mkdir -p t1/t2, however t1 directory already exists at current location.

So, in this tutorial, we discussed Linux mkdir command and we have also covered different examples related to its implementation. Here is the list of topics that we have covered.

  • Linux mkdir Command
  • Creating Multiple Directories
  • Creating Directory from current location to other location
  • Tasks for Practice
  • Creating E4 directory inside E3 directory

Comments are closed.