How to Create a Directory in Linux via mkdir Command

December 1, 2023

Introduction

The mkdir command in Linux/Unix is a command-line utility that allows users to create new directories. mkdir stands for "make directory."

With mkdir, you can also set permissions, create multiple directories at once, and much more.

This tutorial will show you how to use the mkdir command in Linux.

How to create a directory in Linux via the mkdir command - a tutorial.

Prerequisites

  • Linux or UNIX-like system.
  • Access to a terminal/command line.
  • A user with permissions to create and change directory settings.

mkdir Command Syntax in Linux

The mkdir command has the following syntax:

mkdir [options] dir_name
  • The [options] section is optional and modifies mkdir's behavior. The available options are listed in the section below.
  • The dir_name is the name or names of the directories you want to create. You can specify multiple directory names separated by spaces.

Note: Use the cd command to navigate to the directory where you want to create a sub-directory. You can also use the direct path. Additionally, use ls to list the directories in the current location.

mkdir Linux Command Options

The options allow you to modify the mkdir command's behavior. The table below shows the most common mkdir options and their descriptions:

OptionDescription
-pCreates parent directories if they don't exist.
-m a=[rwx] [dir_name]Sets the file modes, i.e., permissions, for the new directory.
-vDisplays a message for each created directory.
--versionDisplays the version number and information about the license and exits.
-ZSets the SELinux security context for directories.
-helpDisplays help with information about mkdir options.

mkdir Linux Examples

This section provides practical examples to help you better understand how mkdir works.

Example 1: How to Create New Directory in Linux

To create a directory using the terminal, pass the desired directory name to the mkdir command.

In this example, we create a directory named Linux. Commands in Linux and options are case sensitive, so pay attention to capitalization:

mkdir Linux

If the operation is successful, the terminal returns an empty line. To verify the directory's creation, use the ls command:

ls -l
Creating a new directory in Linux using mkdir.

Note: To create a hidden directory, follow our guide on how to hide files in Linux.

Example 2: Create Directory in Specific Location

To create a directory or directories in a specific location other than your current working directory, specify the full directory path. For example:

mkdir /tmp/example

The command creates a directory example in the specified location. Let's switch to that directory and check:

cd /tmp
ls -l
Creating a directory in a specific location.

Listing the contents of the directory shows that the directory was created.

Example 3: How to Create Multiple Directories

While it is possible to create directories one by one with mkdir, it can be time-consuming when you need to create more than a few. To avoid that, run a single mkdir command and list the directory names separated by a space.

For example:

mkdir dir1 dir2 dir3

The command above creates the three specified directories.

However, you can also use mkdir with curly brackets {} to create multiple directories. For example, to create several directories, list them within curly brackets, without spaces:

mkdir {test1,test2,test3}

You can also create a batch of directories starting with the same pattern. Prepend the curly brackets with the pattern and use the brackets to specify the number of directories. For example:

mkdir dir{1..15}

The command above creates 15 directories, from dir1 to dir15.

Creating directories in batch with mkdir.

Do not add any spaces in the curly brackets for the directory names. If you do, the names in question will include the extra characters.

Example 4: How to Use Environment Variables in Directory Names

mkdir also allows you to use environment variables in directory names. In the following example, we create a directory with the current user being the directory name:

mkdir $USER
Creating a directory with variables in the name.

Listing the directory contents shows that the directory was created using the current user's name.

Example 5: How to Make Parent Directories

Building a structure with multiple subdirectories using mkdir requires adding the -p option. This ensures that mkdir adds any missing parent directories in the process.

For example, if you want to create dirtest2 in dirtest1 inside the Linux directory (i.e., Linux/dirtest1/dirtest2), run the following command:

mkdir –p Linux/dirtest1/dirtest2

Use ls -R to show the recursive directory tree.

Using mkdir to create parent directories.

Without the -p option, the terminal returns an error if one of the directories in the string does not exist.

Example 6: How to Set Permissions When Making a Directory

The mkdir command gives rwx (read, write, and execute) permissions for the current user only by default.
To add all the permissions for all users, specify the -m option with the user 777 when creating a directory.

Run the following command to create a directory DirM with rwx permissions:

mkdir –m777 DirM

To list all directories and show the permissions sets, run:

ls -l
Using mkdir to set directory permissions.

Example 7: How to Verify Directories

When executing mkdir commands, there is no feedback for successful operations. To see the details of the mkdir process, append the -v option to the command.

Let’s create a Details directory and print the operation status:

Output a message when creating a directory.

By getting the feedback from the process, you do not have to run the ls command to verify the directory was created.

Note: Learn to fully manage directories by learning to move a directory in a system running a Linux distribution.

Conclusion

This guide showed how to use the Linux mkdir command to create new directories via the command line. You can also use the command to set the permissions for the new directory and ensure the right users have read, write, and execute rights.

Next, check out our essential Linux commands tutorial and download a handy PDF cheat sheet.

Was this article helpful?
YesNo
Goran Jevtic
Goran combines his leadership skills and passion for research, writing, and technology as a Technical Writing Team Lead at phoenixNAP. Working with multiple departments and on various projects, he has developed an extraordinary understanding of cloud and virtualization technology trends and best practices.
Next you should read
How to Add User to a Group in Linux
December 1, 2023

By following the steps in this tutorial you will learn how to add a user to a group in Linux. Read the easy...
Read more
Nmap Commands - 17 Basic Commands for Linux Network
May 14, 2019

Nmap stands for Network Mapper. It is an open-source tool for network exploration and security auditing. With ...
Read more
How to List Users in Linux, List all Users Command
August 4, 2022

Linux OS is unique because of its multiuser characteristic. It allows multiple users on one system, at the ...
Read more
How To Use grep Command In Linux/UNIX
February 29, 2024

This guide details the most useful grep commands for Linux / Unix systems. After going through all the ...
Read more