The mkdir Command on Linux

In this tutorial, we will be showing you how to use the mkdir command on a Linux-based operating system.

mkdir command on Linux

mkdir is short for make directory, which immediately should give you an idea of what this command is used for.

The mkdir command is an essential tool to learn how to use on your Linux system, especially when dealing with the terminal.

This tool is the primary way to create directories/folders on Linux-based operating systems. It is a straightforward yet powerful command that you will be able to master in no time.

Over the following few sections, we will show you the various ways that you can use the mkdir command within Linux.

Table of Contents

Syntax of the mkdir Command

The syntax for the mkdir command on Linux is relatively straightforward.

It has two parameters that you can use. One of these is optional and is used to specify options. The other is used to specify directory names.

mkdir [OPTION]... NAME...
  • The “[OPTION]” parameter allows you to control the behavior of the mkdir command on Linux.

    You can use this to tell mkdir to use specified permissions or create parent directories.
  • The “NAME” parameter is where you will specify the directories you want to create.

    mkdir will create the directory within the current working directory if it is only a name.

    If you use a full path, the mkdir command will attempt to create the last directory referenced in that path.

Basic Usage of the mkdir Command on Linux

Let us start by exploring the most basic usage of the mkdir command on Linux. The simplest way to use this command is not to use any options.

To create a directory, you need to use “mkdir” followed by either just the directory name or full path.

mkdir NAME...

Using this command, Linux will attempt to create the specified directory. You can either specify a name or a full path.

If you only used a name, then mkdir will attempt to make the directory in the current working directory.

Alternatively, if you use a full path, Linux will only make the directory at the end of the path. You can use an option to adjust this behavior.

For example, with the path “/example/directory/name/here“, mkdir will only attempt to create the “here” directory. If any other part doesn’t exist, the command will fail.

Example of Creating a Directory in the Current Working Directory

In this example, we will create a directory within the current working directory. You can use the pwd command to see the current working directory.

For our example, our current working directory is “/home/pi“, and the directory we are creating will be called “helloworld“.

You can create this directory by running the following command in the Linux terminal.

mkdir helloworld

To verify that mkdir created this directory, you can either try to change into it using the cd command or use the ls command.

pi@pimylifeup:~ $ ls -l
.......
drwxr-xr-x  2 pi   pi     4096 Feb 28 10:25 helloworld
.......

Example of Creating a Directory with a Full Path

For our example, we will create a new directory within our “pi” user’s home directory called “pimylifeup“.

All we need to do is use Linux’s mkdir command followed by our full pathname, which in our case is “/home/pi/pimylifeup“.

mkdir /home/pi/pimylifeup

As “/home/pi” exists on our example system, the mkdir command will run on our system without a hitch.

To see that mkdir created our new directory let us try using the stat command on it.

pi@pimylifeup:~ $ stat /home/pi/pimylifeup
  File: /home/pi/pimylifeup
  Size: 4096            Blocks: 8          IO Block: 4096   directory
Device: b302h/45826d    Inode: 518938      Links: 2
.......

Example of Attempting to Create a Directory in a Non-Existent Path

For our final example, we will show you what happens when you use mkdir to create a directory in a non-existent path.

While you can work around this by using one of the tools options, you must understand the error.

For this example, let us try creating a directory at “/etc/randomdir/pimylifeup“. Now we know for a fact that our system does not have a directory within “/etc/” called “randomdir“.

mkdir /etc/randomdir/pimylifeup

After using mkdir on a full path where a preceding directory doesn’t exist, you will get an error similar to the one shown below.

mkdir: cannot create directory ‘/etc/randomdir/pimylifeup’: No such file or directory

Create Parent Directories using the mkdir Command on Linux

The mkdir command on Linux allows you to create parent directories if they are missing. You will need to use the “-p” or “--parents” option to achieve this.

The mkdir command will check if each part of a path exists by utilizing this option. If a part of the path doesn’t exist, it will create it.

To use the parents option, use “mkdir“, followed by either “-p” or “–parents”, and your pathname.

mkdir -p PATH

Example of Creating Parent Directories with mkdir

To show you how this works, we will use the mkdir command to create the directories “/etc/randomdir/pimylifeup“.

Even though the “randomdir” directory doesn’t exist, the parents option will now make both it and our “pimylifeup” base directory.

To create this directory, use “mkdir“, followed by the “-p” option, then finally the full path.

mkdir -p /etc/randomdir/pimylifeup

You can verify that your path was created by mkdir by using either the ls, cd, or stat commands.

pi@pimylifeup:~ $ ls -l /etc/randomdir/
total 4
drwxr-xr-x 2 root root 4096 Feb 28 11:58 pimylifeup

The mkdir command will not print any feedback when it successfully creates a directory by default.

If you want this information printed, you will have to use the “-v” or “--verbose” options alongside mkdir. The command will print a message for every directory created using this option.

This is best used alongside the parents option as you can tell which of these directories is created.

mkdir -v NAME...

Using the Verbose option with the mkdir Command on Linux

To show how this works, we will combine the verbose option (-v) alongside the parents option (-p).

For this example, let us create all directories in the following path “/var/pi/my/life/up“. The only part of this path that existed on our system is “/var/“.

mkdir -v -p /var/pi/my/life/up

Below you can see how the mkdir command worked through our path. You can see that it created each of our missing directories.

mkdir: created directory '/var/pi'
mkdir: created directory '/var/pi/my'
mkdir: created directory '/var/pi/my/life'
mkdir: created directory '/var/pi/my/life/up'

Set File Permissions with the mkdir Command on Linux

By default, the mkdir command will create directories based on the umask. The umask tells the mkdir command what permissions should be given to a directory or file when it is created.

The “-m” or “--mode” option allows you to use the sane numeric and symbolic notation supported by the chmod command.

Using the following command, you can set the permissions on a directory during its creation.

mkdir -m MODE NAME...

Example of Setting File Permissions with mkdir

For this example, we will use the mkdir command to create a directory and assign it the permissions of “764“.

The directory that we are creating will be called “examplepi“, and we will be making it in the home directory.

mkdir -m 764 /home/pi/examplepi

After this directory is created, we can run the “ls” command on its parent director to see the new folder and its permissions.

pi@pimylifeup:~ $ ls -l /home/pi/
total 556
......
drwxrw-r--  2 pi   pi     4096 Mar  6 04:35 examplepi
......

From this result, mkdir created our new directory, and the tool applied the new permissions.

Creating Multiple Directories with the mkdir Command

The mkdir command offers you two different ways to create multiple directories with a single command.

For the first way, all you need to do is separate each directory name by a single space.

mkdir NAME1 NAME2 NAME3 ....

Additionally, you can also use the curly brackets ({ }) as an alternative. Finally, each directory name must be separated by a comma (,).

You can create complicated directories by combining this method with the above and the “-p” option.

mkdir {NAME1,NAME2,NAME3,...}

Example of Creating Multiple Directories

For this example, let us create four directories called “pimy“, “lifeup“, “exampledir“, and “tutorial“.

You can create all four of these directories using the mkdir command by using the following.

mkdir pimy lifeup exampledir tutorial

Alternatively, this same command could also be written as shown below.

mkdir {pimy,lifeup,exampledir,tutorial}

You can verify that these directories were created by the mkdir command using the ls command.

pi@pimylifeup:~ $ ls -l
total 572
......
drwxr-xr-x  2 pi   pi     4096 Mar  6 05:04 exampledir
drwxr-xr-x  2 pi   pi     4096 Mar  6 05:04 lifeup
drwxr-xr-x  2 pi   pi     4096 Mar  6 05:04 pimy
drwxr-xr-x  2 pi   pi     4096 Mar  6 05:04 tutorial
......

Conclusion

Throughout this guide, we have gone through the various ways to utilize the mkdir command on Linux.

It is a valuable tool to learn how to use as it is the primary way to create directories.

The two ways you will likely use this command are by only specifying the directory name or using the "-p” option.

Please comment below if you have any questions about using the mkdir tool.

Please also check out our numerous other Linux command guides or our general Linux tutorials.

Leave a Reply

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