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)

FAQs

How to Create a Directory in Linux via mkdir Command? ›

To create new directory use "mkdir" command. For example, to create directory TMP in the current directory issue either "mkdir TMP" or "mkdir ./TMP".

What Linux command creates a new directory? ›

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.

Which command is used to make a new directory? ›

The mkdir (make directory) command in the Unix, DOS, DR FlexOS, IBM OS/2, Microsoft Windows, and ReactOS operating systems is used to make a new directory.

How to create a file in a directory in Linux? ›

First of all, create a directory and named it as New_directory, execute the mkdir command as follows:
  1. mkdir New_directory. Change directory to it:
  2. cd New_directory. Output: ...
  3. cat > test.txt. ...
  4. cat test.txt. ...
  5. touch test1.txt. ...
  6. ls - l test1.txt. ...
  7. touch test1.txt test2.txt test3.txt. ...
  8. touch test4.txt test.odt.

How do I create a parent directory in mkdir? ›

Example of Creating Parent Directories with mkdir

To create this directory, use “ mkdir “, followed by the “ -p ” option, then finally the full path. You can verify that your path was created by mkdir by using either the ls , cd , or stat commands.

How do you create a folder in mkdir? ›

To create new directory use "mkdir" command. For example, to create directory TMP in the current directory issue either "mkdir TMP" or "mkdir ./TMP". It's a good practice to organize files by creating directories and putting files inside of them instead of having all files in one directory.

How to create multiple directory in Linux? ›

To recursively create multiple directories at once within the same parent directory, you can use the following syntax: $ mkdir -p <path> … This command will create the src and tmp directories within the same parent directory, including their intermediate directories public , utils , and logs .

How to create a path in Linux? ›

Linux: Add to PATH Permanently
  1. Open the . bashrc file using a text editor. ...
  2. Go to the end of the file.
  3. Paste the export syntax at the end of the file. export PATH="/Directory1:$PATH"
  4. Save and exit.
  5. Execute the script or reboot the system to make the changes live. To verify the changes, run echo :
Sep 22, 2022

How to create a parent and child directory in Linux? ›

The mkdir command is also used to create multiple directories at time as shown in below. To create a parent directory and child directory using -p or --parents option and print a message that what is being done using -v or --verbose option along with the mkdir command in the Linux system as shown in below.

How do I enter a directory in Linux? ›

To open a directory in a terminal, you use the cd command to change your current directory. This essentially opens that folder and places you in it.

What is the command for parent directory in Linux? ›

To navigate to the parent directory of the current directory using the command-line in Linux, you can make use of the "cd" command along with the ".." notation. The "cd" command is used to change the current working directory, and the ".." notation represents the parent directory.

Which Linux command is used to create a new? ›

The touch command is the most commonly used command for creating a new file in Linux. To create a new file in the current directory, you need to run the touch command followed by the name of the file.

What is the cd command in Linux? ›

The cd command in Linux stands for change directory. It is used to change the current directory of the terminal. The terminal, by default, opens the home directory.

What is the pwd command in Linux? ›

The pwd command writes to standard output the full path name of your current directory (from the root directory). All directories are separated by a / (slash).

How to create a new directory in command prompt? ›

3.2. Creating Directories
  1. Type dir and press <ENTER> again at the command prompt to get the directory listing. Look at the list on your screen. ...
  2. Type the following commands: md John <ENTER> ...
  3. Type dir <ENTER> You can see that the two subdirectories have been created on the C-Drive.
  4. Type dir /AD <ENTER>

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Madonna Wisozk

Last Updated:

Views: 6081

Rating: 4.8 / 5 (48 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Madonna Wisozk

Birthday: 2001-02-23

Address: 656 Gerhold Summit, Sidneyberg, FL 78179-2512

Phone: +6742282696652

Job: Customer Banking Liaison

Hobby: Flower arranging, Yo-yoing, Tai chi, Rowing, Macrame, Urban exploration, Knife making

Introduction: My name is Madonna Wisozk, I am a attractive, healthy, thoughtful, faithful, open, vivacious, zany person who loves writing and wants to share my knowledge and understanding with you.