Mkdir Command in Linux

FREE Online Courses: Transform Your Career – Enroll for Free!

By the end of this article, you will learn all there is to the mkdir command in Linux-based operating systems. We will be going through what mkdir is, a brief history of the command, the different options used with the mkdir command along with the syntax. In the end, we will look at some practical commands of the mkdir commands in the terminal which help in making directories without using the GUI. Let’s start!!!

What is mkdir Command in Linux?

Mkdir is an abbreviation for “Make Directory”. Mkdir is a command-line-based utility in Linux-based operating systems that helps in the creation of directories.

We all know that there are 2 ways of doing things in Linux distributions. One is the GUI and the other is the terminal. we also know that the command line or the terminal is what makes Linux stand out.

The Terminal is all about efficiency and productivity rather than the GIU where you have to click many buttons and navigate through different items. However, in the terminal, there are no extra Clicks on nicks!

Hence we have different file management commands like rm, cp, mv, rmdir, pwd, cd and ls. Mkdir is one such file management command whose role is to create directories from the terminal.

History of Linux mkdir command

In the initial versions of UNIX, as the kernel did not have a mkdir syscall, the command had to be setuid root. Rather it made the directory with mknod and linked in the “.” and “..” directory entries manually.

The mkdir command that comes in the GNU Coreutils was written developed and designed by David MacKenzie.

Syntax of linux mkdir command

The syntax of the mkdir command is pretty simple:

mkdir <options> <path or directory name>

The syntax has 2 fields, one is for the options, which we will look at in-depth in the net section. The other field is where you either enter the name of the directory you want to create or the path of the directory with its name at the end.

Options used with linux mkdir command

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

We can pair the mkdir command with the available options which specify or tell what the mkdir command must do. Let us look at the options available with the mkdir command.

1. -p

This option makes parent directories as needed, and does not print an error if a parent directory exists. You can also write this option as “–parents”

2. -v

This option provides verbose to the output to get a better understanding.

3. -Z

This option sets the SE Linux security context of each created directory to the context you specified. You can also write this option as “–context”.

4. -m

This option sets the file mode, it functions very similar to the chmod command. You can also write this option as “–mode”

5. –size <n>

This option uses the specified size in kilobytes as the size of the image to use for the boot disk. If you don’t specify anything, it will take the default value as 1.44Mb

6. –kernelargs <arguments>

This option specifies the arguments appended on the kernel command line.

7. –help

This option prints the following help menu.

help menu of the mkdir command

8. –version

This option prints the information about the version of mkdir you are using

version of the mkdir command

Now that we have laid down the fundamentals, let us look at some practical commands of the mkdir command in the terminal

Creating a directory

The process of creating a directory is extremely easy, all you gave to do is use the mkdir command followed by the name of the directory you want to create. For example in the below output, I create a directory called “Ferrar” by using the following syntax:

mkdir <directory name>

creating a directory

But there is one disadvantage to this method. You need to be in the directory where you want to create your directory. So before using the mkdir command, you need to navigate to the specific directory by using the “cd” command as shown:

navigating to the a specific directory to create a directory in it

Creating a directory using a path

We already know that the terminal is way better than the GUI. We can create a directory in any specific location, no matter where you are in the filesystem. You can do so by specifying the path of where you want the directory to be created followed by the name of the directory you want to create:

mkdir <path>

creating a directory using a path

In the above output, I created a directory called “bentley” in the directory “cars”, which is in the path “/home/gopikiran/Desktop/cars” even though I was in the home directory.

You must note that when you are specifying the path of the directory, always start from the root directory (/) and work your way down.

Creating multiple directories

To create multiple directories using the mkdir command, you need to specify the names of the directories after the mkdir command. Make sure that the names of the directories are enclosed in a curly bracket and also separated by commas as shown below:

mkdir {directory1, directory2, directory3, directory4}

Let us understand this better with an example:

creating multiple directories

Providing verbose while creating directories

Until now, in all of the outputs above, we have been checking if the directory has been successfully created or not by using the “ls” command. Instead, of using a whole other command another time, we can provide verbose to the output by using the option “-v”, and mkdir will show us if the directory has been created or not.

providing verbose while creating a directory

In the below-shown output, If I try to create the same directory in the same path twice, I get a message saying that the directory already exists and I cannot create it.

providing verbose while creating a directory using a path

You can even provide verbose while creating multiple files as shown:

providing verbose while creating multiple directories

Creating parent directories

Using the mkdir command, you can even create subdirectories within the directory you are creating. Meaning, that you are creating a subdirectory at the same time you are creating the directory.

The best part is that you can create how many ever subdirectories you want! To do so, use the option p and separate the subdirectories by a slash (/) as shown:

mkdir -p <directory name>/<subdirectory name>.........

Let us understand this by looking at an example.

creating parent directories

In the above output, I created a directory “bentley” inside the directory “lambo”, which in turn inside the directory “cars”, which is inside the directory “pictures”.

Setting permissions while creating a directory

Just like the ‘chmod’ command in Linux, mkdir set the permissions of a directory, except mkdir takes things 1 notch higher, we can immediately set the permissions as soon as we create one. Before we see how to create permissions using mkdir, let us give a light brush up on permissions.

Basically speaking permission is 3 sets of characters (a total set of 6 characters). These characters are: r (read), w(write) and x (execute). The first set of 3 characters are the permissions for the user, the second is for the user group and the third is for the other users. Below is the table of permissions in octal notation.

Binary NotationOctal NotationPermissionmeaning
0000No permission
0011–xOnly execute
0102-w-Only write
0113-wxWrite and execute
1004r–Only read
1015r-xRead and execute
1106rw-Read and write
1117rwxRead write and execute

To set permissions using the mkdir command use the option “-m” followed by the set of permissions in octal form as shown below:

mkdir -m <permissions> <directory name>

For example, the command “mkdir -m 777 cars” will create a directory named “car” and gives it the permission or reading, writing and executing to the user, group and other users (rwxrwxrwx).

setting permission of 777 while creating a directory

 

Another way of writing the above command is by using the command “mkdir -m a=rwx safari”, where “a” stands for “all”, thereby giving everyone the permissions of rwx.

another way of giving 777 permissions while creating a directory

The command “mkdir -m 740 safari” will give the permission of reading, writing, and executing to the user, only reading to the group members and no permissions to the other users (rwxr—–).

setting permission of 740 while creating a directory

Summary

As you have seen the mkdir command is a really simple file management command in Linux that helps in creating directories from the terminal. Using the mkdir improves the efficiency of workflow a lot as it can do a lot more stuff than the GUI

You have now learned what mkdir is, its syntax options, and practical commands in the terminal.

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *