How to Use mkdir Command to Make or Create a Linux Directory

April 8, 2019

What is the mkdir Command in Linux?

The mkdir command in Linux/Unix allows users to create or make new directories. mkdir stands for “make directory.”

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

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

header image How to Create Directories in Linux with the mkdir Command

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 basic command for creating directories in Linux consists of the mkdir command and the name of the directory. As you can add options to this command, the syntax looks like this:

mkdir [option] dir_name

To understand better how to use mkdir, refer to the examples we provide in the rest of the guide.

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

How to Make a New Directory In Linux

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

In this example, we created a directory Linux on the desktop. Remember commands in Linux and options are case sensitive.

mkdir Linux

If the operation is successful, the terminal returns an empty line.

To verify, use ls.

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

How to Create Multiple Directories with mkdir

You can create directories one by one with mkdir, but this can be time-consuming. To avoid that, you can run a single mkdir command to create multiple directories at once.

To do so, use the curly brackets {} with mkdir and state the directory names, separated by a comma.

mkdir {test1,test2,test3}

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:

How to Make Parent Directories

Building a structure with multiple subdirectories using mkdir requires adding the -p option. This makes sure 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 command:

mkdir –p Linux/dirtest1/dirtest2
example of creating a new directory in linux

Use ls -R to show the recursive directory tree.

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

error message in terminal: directory does not exist or cant be found

How to Set Permissions When Making a Directory

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

To create a directory DirM with rwx permissions:

mkdir –m777 DirM
example how to create a directory with the mkdir command in terminal

To list all directories and show the permissions sets: -l

The directory with rwx permissions for all users is highlighted. As you can see on the image above, two other directories by default have rwx permission for the owner, xr for the group and x for other users.

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 terminal command.

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

example for details of new directory made in linux

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

mkdir Command Options and Syntax Summary

Option / SyntaxDescription
mkdir directory_nameCreates a directory in the current location
mkdir {dir1,dir2,dir3,dir4}Creates multiple directories in the current location. Do not use spaces inside {}
mkdir –p directory/path/newdirCreates a directory structure with the missing parent directories (if any)
mkdir –m777 directory_nameCreates a directory and sets full read, write, execute permissions for all users
mkdir –v directory_name(s)Creates a directory in the current location

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

Conclusion

This guide covered all commands you need to create directories in Linux.

Now you understand how to use the Linux mkdir command. It’s straightforward and simple to use.

If you have the necessary permissions, there should be no error messages when you follow the instructions in this article.

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
April 8, 2019

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
March 28, 2019

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