Beginners Guide for Mkdir Command on Linux

Linux TLDR
Last Updated:
Reading time: 3 minutes

In UNIX/Linux, the mkdir command is used to create single or multiple directories (also referred to as folders in some operating systems) in the current working directory.

If the specified directory is already present in the current working directory, then the β€œFile exists” error will be thrown unless the β€œ-p” flag is not assigned.

Apart from that, you can also specify the permission in character or numeric while creating the directory, but to perform all this action, you should have proper permissions.

In this article, you will learn all about the mkdir command with practical examples.

Tutorial Details

DescriptionMkdir
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitesmkdir
Internet RequiredNo

Syntax of the Mkdir Command

The mkdir command takes two arguments: one is the option, and the other is the directory name.

$ mkdir [OPTION] [DIRECTORY]

The following is the list of known options:

OptionsDescription
"-m", "--mode=MODE"Set permissions while creating a new directory (like the chmod command).
"-p", "--parents"It will create a parent directory and will not throw an error if the specified directory is already present.
"-v", "--verbose"Print the message for each directory created.
"-Z"Set the SELinux security context of each directory to the default type.
"--context[=CTX]"Indentical to β€œ-Zβ€œ, or if CTX is specified, then set the SELinux or SMACK security context to CTX.
"--help"Display the help section.
"--version"Display the binary version.

Creating a New Directory

The following command, without any flag, will create a single directory with the specified name in your current working directory.

$ mkdir mydir

Output:

Creating a new directory in the current working directory

Creating Multiple Directories

Instead of one, specify multiple directories separated by a space to create them in your current working directory.

$ mkdir mydir1 mydir2 mydir3

Output:

Creating multiple directories in the current working directory

Creating Multiple Directories Using Special Characters

Unlike giving a space, you can specify multiple directories within the β€œ{}” curly bracket, separated with a β€œ,” comma, to create them in your current working directory.

$ mkdir {mydir1,mydir2,mydir3}

Output:

Creating multiple directories using special characters in the current working directory

Creating a New Directory in the Desired Location

Define the absolute or relative path to create a new directory in your desired location (restricted locations require root or sudo privileges).

$ mkdir ~/Documents/newdir

The above command will create a new directory with the name β€œnewdir” in the β€œ~/Documents” directory.

Creating a new directory at the desired location

Creating a New Directory in Two Different Desired Locations

Same as previous command, specify the complete path of two desired locations with the new directory name separated with a space.

$ mkdir ~/Documents/newdir ~/Downloads/newdir

Output:

Creating a new directory at two different desired locations

As you can see, the β€œnewdir” directory is created inside both the β€œ~/Documents” and β€œ~/Downloads” directories.

Creating Multiple Directories Using Special Characters at Your Desired Location

Again, specify the complete path with multiple directories’ names inside the β€œ{}” curly bracket with a β€œ,” comma as a separator.

$ mkdir ~/Documents/{mydir1,mydir2,mydir3}

Output:

Creating multiple directories using special characters at the desired location

Ignoring the Error if the Specified Directory is Already Present

If you are creating a new directory and a directory with the same name already exists, then you will get the β€œFile exists” error, as shown.

$ mkdir newdir
$ mkdir newdir

Output:

"File exists" error

In this scenario, you can use the β€œ-p” flag; it will create a new directory only if the directory is not present in your current working directory; otherwise, it will exit the command without throwing any errors.

$ mkdir -p newdir
$ mkdir -p newdir

Output:

Create a new directory even if it is already present

Creating a Parent Directory While Creating a New Directory

When you create a new directory inside another directory, if the parent directory does not exist, you will get the β€œNo such file or directory” error.

$ mkdir dir1/dir2/dir3

Output:

"No such file or directory" error

As you can see above, the β€œdir1” directory, including the β€œdir2” directory, is not present, which is why you got an error.

To avoid this error, simply use the β€œ-p” flag to create a new directory that includes all parent directories.

$ mkdir -p dir1/dir2/dir3

Output:

Creating a new directory including its parent directory

Creating a Sub-Directory Inside the Newly Created Multiple Directories

The following command will create a new sub-directory with the name β€œdata” inside all three directories β€œmydir1β€œ, β€œmydir2β€œ, β€œmydir3β€œ, as shown.

$ mkdir -p ~/Documents/{mydir1,mydir2,mydir3}/data

Output:

Creating new sub-directory inside all parent directories

Printing the Directory Name While They Are Created

Use the β€œ-v” flag to print each directory name while it is being created in the output.

$ mkdir -v -p ~/Documents/{mydir1,mydir2,mydir3}/data

Output:

Printing each directory while they are created

Setting Permissions While Creating a New Directory

Use the β€œ-m” flag to set the file mode (or permission) while they are created, as shown.

$ mkdir -m g=rwx ~/Documents/newdir

The above command will grant the directory owner and group of users complete permission.

Giving permission while creating a new directory

Instead of attributes, you can also specify the permissions in numeric values.

$ mkdir -m 777 ~/Documents/newdir

Output:

Giving the permission in numeric value while creating a new directory

This way, you can easily manage to give separate permissions for the owner, a group of users, and others.

And that was all you should know about this command.

If you have any question or query regarding this topic, feel free to ask it in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.