How to Create a Directory in Linux via mkdir Command (2024)

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 mkdir Command (1)

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
How to Create a Directory in Linux via mkdir Command (2)

Note: To create a hidden directory, follow our guide on how to show and create hidden 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 /tmpls -l
How to Create a Directory in Linux via mkdir Command (3)

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.

How to Create a Directory in Linux via mkdir Command (4)

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
How to Create a Directory in Linux via mkdir Command (5)

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.

How to Create a Directory in Linux via mkdir Command (6)

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
How to Create a Directory in Linux via mkdir Command (7)

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:

How to Create a Directory in Linux via mkdir Command (8)

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.

How to Create a Directory in Linux via mkdir Command (2024)

References

Top Articles
Latest Posts
Article information

Author: Duane Harber

Last Updated:

Views: 6069

Rating: 4 / 5 (51 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Duane Harber

Birthday: 1999-10-17

Address: Apt. 404 9899 Magnolia Roads, Port Royceville, ID 78186

Phone: +186911129794335

Job: Human Hospitality Planner

Hobby: Listening to music, Orienteering, Knapping, Dance, Mountain biking, Fishing, Pottery

Introduction: My name is Duane Harber, I am a modern, clever, handsome, fair, agreeable, inexpensive, beautiful person who loves writing and wants to share my knowledge and understanding with you.