Linux mkdir Command Examples

Brief: In this guide, we will take a look at the mkdir command which is used to create a directory. We will also discuss some of the practical examples of it that will help beginners to operate the Linux system confidently.

As Linux users, we use files and directories on a regular basis. Files allow us to store important data whereas directories allow us to organize files in a proper way. In addition to this, we often create a hierarchical directory structure to organize the contents in a better way.

In this beginner-friendly article, we will learn about the mkdir command. As the name suggests, the mkdir command is used to create a named directory at a given path, which also allows us to create single or multiple directories at once with the required file permissions.

We should note that to use the mkdir command the user must have the required permissions on the parent directory, or else the command will fail with the permission denied error.

Just like other Linux commands, the syntax of the mkdir command is mainly divided into two groups – options and arguments:

$ mkdir [OPTIONS] ... <DIRECTORY1> <DIRECTORY2> ...

In the above syntax, the square brackets ([]) represent the optional arguments whereas angular brackets (<>) represent the mandatory arguments.

Basic Usage of mkdir Command in Linux

As the name implies, the mkdir is a short form of the “make directory”. The good thing is that it creates a directory only if a directory or file with the same doesn’t exist at the given path. In this way, this is a very safe command and doesn’t cause any harm to the system.

In this section, we will see the basic usage of the mkdir command with examples.

1. Create a Directory in Linux

One of the fundamental use of the mkdir command is to create a named directory at a given path. So let’s create a directory with the name rpm-distros in the current working directory:

$ mkdir rpm-distros

Now, use the ls command to verify that the directory has been created:

$ ls -l

In the first example, we used the relative path with the mkdir command. However, this command also supports the absolute path.

We can use the pwd command or the pwd environment variable to find the absolute path of the current working directory.

So, let’s create the named directory – deb-distros in the current working directory using the absolute path:

$ mkdir $PWD/deb-distros

Now, verify that the new directory has been created in the current working directory:

$ ls -l
Create Directory in LinuxCreate Directory in Linux
Create Directory in Linux

2. Create Multiple Directories in Linux

The mkdir command accepts multiple paths as an argument, which allows us to create multiple directories in one go.

Let’s create three directories inside the deb-distros directory using the single command:

$ mkdir deb-distros/kali deb-distros/mint deb-distros/ubuntu

Now, let’s list the contents of the deb-distros directory:

$ ls -l deb-distros

As we can see, the mkdir command created multiple directories successfully.

Create Mulitple Directories in LinuxCreate Mulitple Directories in Linux
Create Mulitple Directories in Linux

3. Create Multiple Directories Using the Brace Expansion

In the previous example, we saw how to create multiple directories inside another directory using a single command. However, that wasn’t the most efficient way because we specified the parent directory name i.e. deb-distros with each sub-directory.

To overcome this limitation, we can specify the sub-directory names in a brace expansion as shown in the following example, where we create three sub-directories inside the rpm-distros directory:

$ mkdir rpm-distros/{alma,centos,fedora}

Here, we should note the following two important points:

  • There aren’t any spaces on either side of the comma (,).
  • The brace expansion feature is available in the Bash shell only hence this approach is less portable.

Now, let’s verify that the required directory structure has been created successfully:

$ ls -l rpm-distros
Create Multiple Directories Using Curly BracesCreate Multiple Directories Using Curly Braces
Create Multiple Directories Using Curly Braces

4. Create a Nested Sub-Directory Structure

In the previous sections, we saw how to create multiple directories. However, that approach doesn’t work if we wish to create a nested directory structure. In such a case, we can use the -p option of the command that creates the parent directory if required.

Let’s, create a nested sub-directory structure:

$ mkdir -p rpm-distros/centos/8.x/8.1/8.1-1911

Now, verify the contents of the rpm-distros/centos directory in a recursive way:

$ ls -1R rpm-distros/centos
Create Nested Sub-Directory StructureCreate Nested Sub-Directory Structure
Create Nested Sub-Directory Structure

As we can see, the command created the required directory structure without reporting the error for the existing parent directories. This option comes in very handy while writing shell scripts. We can use it to suppress the directory creation error which might occur due to the existing directory.

5. Create a Directory with Permissions

Sometimes, we need to modify the access permission of the directory immediately after its creation. In that case, we have to use the two commands – mkdir and chmod. However, we can achieve the same result using a single command.

Let’s use the -m option to set access permissions on a directory while creating it:

$ mkdir -m 777 dir-1

In this example, we used the numeric format to set the access permission. In a similar way, we can use the textual format.

For example, we can achieve the same result using the below command:

$ mkdir -m a=rwx dir-2

Now, use the ls command to find out the access permission of the directories:

$ ls -ld dir-2 | awk '{print $1}'
Create Directory with Permission in LinuxCreate Directory with Permission in Linux
Create Directory with Permission in Linux

6. Enable Verbose with mkdir Command

By default, the mkdir command doesn’t print anything on the terminal after the directory creation. Hence, so far we have been using the ls command to verify whether or not the directory has been created.

To overcome this limitation, we can use the verbose mode of the command that prints the message for each created directory. This option gives meaningful information when we combine it with the –p option:

Let’s use the -v option with the command to enable the verbose mode:

$ mkdir -p -v dir-1/dir-2/dir-3/dir-4/dir-5

Now, let’s observe the output of the command:

Create Directory with Verbose ModeCreate Directory with Verbose Mode
Create Directory with Verbose Mode

In this article, we saw the basic usage of the mkdir command. First, we saw how to create a single directory as well as multiple directories. Next, we saw how to set permissions on a directory while creating it. Finally, we saw how to verify the directory creation using the verbose mode.

Do you know of any other best example of the mkdir command in Linux? Let us know your views in the comments below.

Leave a Comment