How to Create Directories in Linux Using the mkdir Command

Updated on November 21, 2023
How to Create Directories in Linux Using the mkdir Command header image

Introduction

The mkdir command enables the creation of directories in a file system. It's also known as make directory (mkdir) and allows you to create directories and sub-directories to organize the file system structure.

This article explains how to use the mkdir command in Linux to create and manage directories.

mkdir Command Syntax

Below is the basic mkdir command syntax:

console
$ mkdir [options] directory_name...

Within the above command, [options] includes optional flags that modify the command behavior, and directory_name... specifies the name of the directory to create. You can center multiple names separated by a comma , to create multiple directories simultaneously.

mkdir Command Options

Option Description
-p Creates parent directories.
-m Sets permissions for the created directory.
-v Displays a message for each newly created directory.

Practical Examples of the mkdir Command

  1. Create a new directory.

    console
    $ mkdir my_directory
    

    The above command creates a new directory my_directory in your active working directory.

    Output:

    mkdir command

  2. Create multiple directories.

    console
    $ mkdir dir1 dir2 dir3
    

    The above command creates three new directories dir1, dir2, and dir3 in the active working directory.

    Output:

    mkdir multiple dir command

  3. Create a new directory with specific permissions.

    console
    $ mkdir -m 755 dir4
    

    The above command creates a new directory dir4 with the 755 permissions level that enables read, write, execute privileges for the owner, and read, execute privileges for the group and other system users.

    Output:

    Dir with permission command

  4. Create a parent directory.

    console
    $ mkdir -p /path/to/parent/dir/new_directory
    

    The above command creates a new directory new_directory within the /path/to/parent/dir directory. If parent directory does not exist, it's created.

    Output:

    Parent dir command

  5. Create a new directory and output the command progress.

    console
    $ mkdir -v my_directory
    

    The above command creates a new directory my_directory and displays the operation's progress.

    Output:

    Create dir with message command

  6. Create nested directories.

    console
    $ mkdir -p project/{src,docs,tests}
    

    The above command creates a nested directory structure with the sub-directories src, docs, and tests using the brace expansion method.

    Output:

    Nested dir command

Advanced Usage Scenarios

  1. Create a new directory with specific permissions recursively.

    console
    $ mkdir -m 755 -p /path/to/parent/dir/new_directory
    

    The above command creates a new directory new_directory in /path/to/parent/dir with the 755 permission level to create the necessary parent directories.

    Output:

    Dir with specific permission command

  2. Create new directories with a sequential naming scheme.

    console
    $ mkdir dir{1..5}
    

    The above command creates five directories dir1, dir2, dir3, dir4, and dir5 using the brace expansion method.

    Output:

    Sequentially named dir command

  3. Create a new directory with spaces in its name.

    console
    $ mkdir "my directory"
    

    The above command creates a new directory my directory with a space in its name.

    Output:

    Dir with spaces name command

  4. Create new directories with a specific date or timestamp.

    console
    $ mkdir "$(date +%Y-%m-%d)"
    

    The above command creates a new directory with the current system date.

    Output:

    Dir with date command

  5. Create new directories using a loop.

    console
    $ for i in {1..5}; do mkdir "dir_$i"; done
    

    The above command creates five directories dir_1, dir_2, dir_3, dir_4, and dir_5 using a loop function.

    Output:

    Dir using loop command

  6. Create a new directory with specific permissions and ownership privileges.

    console
    $ mkdir -m 770 /home/user/secure_data && sudo chown user:group /home/user/secure_data
    

    The above command creates a new directory secure_data with the 770 permissions level with ownership privileges to user and group.

    Output:

    Dir with permissions and ownership

  7. Create new directories and sub-directories with detailed messages.

    console
    $ mkdir -pv projects/{frontend,backend}/{src,dist}
    

    The above command creates a complex directory structure with detailed messages for each new directory.

    Output:

    Complex dir structure

Conclusion

You have used the mkdir command to perform directory creation and management tasks. For more command options, run the man mkdir command to view the mkdir manual page.