Linux Commands

How to Use the mkdir Command in Linux

The mkdir command is used to make new directories in Linux.  In this article, I am going to show you how to use the mkdir command to create directories from the command line in Linux. I am going to use Ubuntu 18.04 LTS for the demonstration, but any Linux distribution should be fine to try out the examples given here. So, let’s get started.

Absolute Path and Relative Path:

There are 2 types of path in Linux. Absolute path and relative path. Having clear concepts of these terms are essential to working with the mkdir command.

Absolute path: It is the full path to your desired directory or file. An absolute path contains the / (root) directory first and then moves downward the directories hierarchy (tree) until your desired directory or file is reached.

For example, /db/mysql/data is an absolute directory path. /etc/fstab is an absolute file path.

Relative path: Relative path is calculated from the current working directory. It may start with or without ./ but it can’t start with /

You can print the current working directory path with the pwd command as follows:

Now, if the directory path is ./db/mysql/data or simply db/mysql/data then, it’s actually inside the parent directory /home/shovon. Notice how we don’t have to type in the full or absolute path /home/shovon/db/mysql/data.

Absolute paths make working with mkdir command a lot easier.

Creating a Directory with mkdir:

This is the simplest and the most common usage of mkdir. You can create a new directory with mkdir very easily.

To create a new directory files/ (let’s say) in your current working directory, run the following command:

$ mkdir files

As you can see, a new directory files/ is created.

$ ls -l

If you don’t want to create a directory in your current working directory, you can of course navigate to the parent directory (inside where you want to create the new directory) and create a new directory as shown above. But there is a better solution.

To create a new directory without navigating to the parent directory, you can specify the directory path as follows:

$ sudo mkdir /mnt/iso

This command creates a new directory iso/ inside the /mnt directory. I didn’t have to navigate to the /mnt directory as you can see.

$ ls -l /mnt

Creating a Directory along with Parent Directories with mkdir:

In the earlier section, I showed you how to create a new directory using the absolute path. But, if the parent directory does not exist, then you won’t be able to create a new directory this way. mkdir would throw an error. You will have to create the parent directory first and then create the desired directory.

Luckly, mkdir has a -p or –parents flag which will create the necessary parent directories if they do not exist.

For example, let’s say, you want to move the MySQL data directory to /db/mysql/data. But none of the parent directories exist at the moment. So, instead of using 3 mkdir command, you can run a single command to tell mkdir to create the parent directories as needed.

$ sudo mkdir -p /db/mysql/data


$ sudo mkdir --parents /db/mysql/data

As you can see, the parent directories are created as required.

The mkdir with the -o flag can also create parent directories automatically for relative paths as well.

For example, let’s say, you want to create a new directory projects/python-test in your current working directory. To do that, run mkdir as follows:

$ mkdir -p projects/python-test

As you can see, the projects/python-test directory is created inside the current working directory.

Creating Multiple Directories with mkdir:

You can create multiple directories with a single mkdir command if you want.

Let’s say, you want to create 3 directories nodejs/, python/, java/ inside your current working directory ~/projects. To create all these directories, run mkdir as follows:

$ mkdir nodejs python java

As you can see, the required directories are created.

Again, let’s say, you want to create multiple directories for the mysql, mariadb, mongodb databases using absolute path. You also want to create any parent directories as required. To do that, run mkdir command as follows:

$ sudo mkdir -p /db/{mysql,mariadb,mongodb}/data

As you can see, the required directories are created all at once.

Here, the directory paths are /db/mysql/data, /db/mariadb/data, /db/mongodb/data. What changes in each directory path? Well, the second level directory mysql, mariadb, mongodb. So, we can put it inside curly braces without any whitespace as /db/{mysql,mariadb,mongodb}/data. Pretty simple.

Verbose Mode of mkdir:

By default, mkdir does not print any message on the screen if the command runs successfully. Only error messages are printed. If you want mkdir to print messages on the screen, then you can use the -v flag or the –verbose flag.

The same command in the earlier section can be written as follows as well.

$ sudo mkdir -pv /db/{mysql,mariadb,mongodb}/data


$ sudo mkdir --parents --verbose /db/{mysql,mariadb,mongodb}/data

As you can see, mkdir now prints message as directories are created.

So, that’s how you use the mkdir command in Linux. Thanks for reading this article.

About the author

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.