How to Use mkdir Command to Create Directories in Linux

How to Use mkdir Command to Create Directories

The mkdir (Make Directory) command creates directories (referred to as folders on some operating systems) from the command-line. When used with the right options, the mkdir command can be a fairly versatile way of creating directories.

In this article we will go through various ways of creating directories using the mkdir command, as well as how to use most of its options.

Prerequisites

To follow along with this tutorial we recommend having:

  • A Linux or Unix-like operating system
  • Having access to a user with appropriate permissions to create directories and change their settings

mkdir Command Syntax

mkdir [options] [directories]

A basic example of creating a folder, without any options, with mkdir is:

mkdir mydir

mkdir can create multiple directories from a single command, and it will create the directories in the order which the directory names are provided:

mkdir mydir_1 mydir_2 mydir_3

mkdir creates the directory if the directory does not already exist. If the directory already exists, it returns an error:

mkdir mydir_1
Output
mkdir: cannot create directory 'mydir_1': File exists

When provided only the directory name, instead of the full path, then the directory will be created in the current working directory.

To create a directory in another location than the current working directory, then you’ll need to provide mkdir with the full path where the directory should be created.

For example, if we’re in the /home folder, and want to create a folder in /var/www, then we’d run:

mkdir /var/www/mydir

It is necessary for the user to have write permissions on the parent directory.

If it does not have sufficient permissions to on the parent directory where you’re trying to create a directory, then you’ll receive a Permission denied error:

mkdir /root/mydir
Output
mkdir: cannot create directory '/root/mydir': Permission denied

mkdir Recursively Create Directories (-p Parent Directories)

mkdir can also recursively create directories using the -p option. This is primarily referred to as creating parent directories, if they don’t exist already.

For example, we may want to create a directory mydir inside another directory anotherdir, but anotherdir doesn’t already exist.

The following command will create the anotherdir directory, along with onemoredir inside it, and mydir inside that.

mkdir -p /anotherdir/onemoredir/mydir

To better see the directories we created we can use the tree command , which is a recursive directory listing utility.

Output
anotherdir/
└── onemoredir
    └── mydir

Any parent directory created through this method, that didn’t already exist, will have its permissions set to =rwx,u+wx by default.

Any existing parent directories will not have their permissions changed.

If you also use the -m option to set directory permissions upon creation, it will not affect file permissions for the newly created parent directories.

mkdir Create Multiple Directories

You can create directories using mkdir in a few ways.

A simple way where you just specify the directory names as the command arguments, separated by spaces:

mkdir mydir1 mydir2 mydir3 mydir4

mkdir Create Multiple Directories using Brace Expansion

Brace expansion is a mechanism by which arbitrary strings can be generated from the command-line or in bash scripts.

Basically, brace expansion allows you to generate multiple command-line arguments, from a simple argument that you’d write with the appropriate syntax for brace expansion.

The syntax for brace expansion of either a sequence specification or a comma separated list of items inside curly braces {}. A sequence consists of a starting and ending item, separated by two periods (..), such as {A..Z} or {1..10}.

First, let’s see some examples of what brace expansion looks like.

Comma Saparated Lists: This is similar to the basic method of creating multiple directories. It’s important that *you do not add any spaces* between directory names and commas, otherwise the spaces will be included in the directory names.

{dir1,dir2,dir3}          => dir1 dir2 dir3
{"dir 1","dir 2","dir 3"} => 'dir 1' 'dir 2' 'dir 3'

Ranges: You can use ranges of numeric and/or alphabetic characters.

{A..E}       => A B C D E
{2..9}       => 2 3 4 5 6 7 8 9
{A..C}{1..3} => A1 A2 A3 B1 B2 B3 C1 C2 C3

Preamble: A preamble is like a prefix, that you can use with ranges and comma separated lists.

dir{1..3}  => dir1 dir2 dir3
dir{1,7,8} => dir1 dir7 dir8

Postscript: A postscript is like a suffix, that you can use with ranges and comma separated lists.

{1..3}dir  => 1dir 2dir 3dir
{1,7,8}dir => 1dir 7dir 8dir

Nested: You can also nest ranges in comma separated lists.

{a,b{1..3},c} => a b1 b2 b3 c

To demonstrate how to use mkdir with brace expansion, we’ll go over a few examples using verbose mode (-v).

Create directories with comma separated lists.

mkdir -v {dir1,dir2,dir3}
Output
mkdir: created directory 'dir1'
mkdir: created directory 'dir2'
mkdir: created directory 'dir3'

Recursively creating directories with preamble, ranges, postscript, and nesting inside a comma separated list.

mkdir -pv parentdir/{{1..2}/dir_{A..C}_2021,{3..4}/dir_{A..C}_2022}
Output
mkdir: created directory 'parentdir'
mkdir: created directory 'parentdir/1'
mkdir: created directory 'parentdir/1/dir_A_2021'
mkdir: created directory 'parentdir/1/dir_B_2021'
mkdir: created directory 'parentdir/1/dir_C_2021'
mkdir: created directory 'parentdir/2'
mkdir: created directory 'parentdir/2/dir_A_2021'
mkdir: created directory 'parentdir/2/dir_B_2021'
mkdir: created directory 'parentdir/2/dir_C_2021'
mkdir: created directory 'parentdir/3'
mkdir: created directory 'parentdir/3/dir_A_2022'
mkdir: created directory 'parentdir/3/dir_B_2022'
mkdir: created directory 'parentdir/3/dir_C_2022'
mkdir: created directory 'parentdir/4'
mkdir: created directory 'parentdir/4/dir_A_2022'
mkdir: created directory 'parentdir/4/dir_B_2022'
mkdir: created directory 'parentdir/4/dir_C_2022'

Let’s also check the tree structure of the created folders:

tree
Output
.
└── parentdir
├── 1
│   ├── dir_A_2021
│   ├── dir_B_2021
│   └── dir_C_2021
├── 2
│   ├── dir_A_2021
│   ├── dir_B_2021
│   └── dir_C_2021
├── 3
│   ├── dir_A_2022
│   ├── dir_B_2022
│   └── dir_C_2022
└── 4
├── dir_A_2022
├── dir_B_2022
└── dir_C_2022

As you can see, there’s quite a bit of variation, and using mkdir with brace expansion might save you some time in some scenarios.

mkdir Set Directory Permissions (-m mode)

mkdir can create directories and assign them permissions at the same time, using the -m or --mode option.

The syntax for permissions is the same as [chmod](https://bytexd.com/change-file-folder-permissions-linux-chmod/), so you can use both symbolic and numerical notations.

By default, the permissions set on directories created with mkdir are read, write and execute for the owner, along with read and execute for the group and users. This may differ on some systems, as permissions depend on the umask, which is used to assign the default file permission sets for newly crated folders and files.

The following command will create a directory called mydir_1 and assign it read, write and execute permissions for everyone:

mkdir -m 'a=rwx' mydir_1

Let’s check the directory’s permissions by running the ls -l command:

Output
drwxrwxrwx 2 edxd edxd 4096 Dec 3 18:28 mydir_1

As you can see, permissions are drwxrwxrwx, which means it’s a directory (d) and has read, write, and execute permissions (rwx) for all users (owner, group, other users).

Let’s also see an example of setting permissions using numeric notation.

In this example we’ll create a directory and set permissions so only the owner has read, write and execute permissions (7), and the group and other users have none (0):

mkdir -m 700 mydir

This option only affects the provided directory given in the command line, and does not affect the parent directories created using the -p option.

mkdir Verbose to Check Created Directories (-v)

mkdir can output messages for each created directory. Since mkdir doesn’t return any message when successully creating directories, the verbose (-v) is a great way to get feedback.

This can be especially useful when creating directories recursively with the -p option.

As with other Linux commands, we can combine options so they’re more convenient to write. Instad of -p -v we can use -pv.

This command will output messages with the created parent directories:

mkdir -pv anotherdir/onemoredir/mydir
Output
mkdir: created directory 'anotherdir'
mkdir: created directory 'anotherdir/onemoredir'
mkdir: created directory 'anotherdir/onemoredir/mydir'

In addition to the verbose option, to better illustrate the created directories, we can use the tree utility to display a tree view of our new directories. This can be especially useful when checking recursively created directories:

tree
Output
.
└── anotherdir
    └── onemoredir
        └── mydir

You can also use the du command to check the created directories, if you don’t have tree installed:

du -h
Output
4.0K ./anotherdir/onemoredir/mydir
8.0K ./anotherdir/onemoredir
12K ./anotherdir
16K .

If we run the mkdir command without the -p option we’ll get an error:

mkdir anotherdir/onemoredir/mydir
Error Output
mkdir: cannot create directory '/anotherdir/onemoredir/mydir': No such file or directory

mkdir Exit Code

mkdir returns an exit status of zero (0) to indicate the success of the command, and a nonzero value to indicate failue.

This is useful when you’re writing a script and need to do error checks.

We can do two tests of mkdir succeeding and failing, and then print the exit status using echo $?, which prints the exit status of the last executed command.

When mkdir successfully creates a directory it will return the exit status of zero (0).

mkdir: created directory 'anotherdir'
mkdir: created directory 'anotherdir/onemoredir'
mkdir: created directory 'anotherdir/onemoredir/mydir'
Print exist status of last executed command
echo $?
Exit Code
0

When mkdir fails in in creating a directory it will return the exit status of 1.

Error Output
mkdir: cannot create directory '/anotherdir/onemoredir/mydir': No such file or directory
Print exist status of last executed command
echo $?
Exit Code
1

Conclusion

In this tutorial we learned how to use the mkdir command and various ways on how to create directories with it, as well as how to use most of its’ options.

If you encounter any issue or have any questions feel free to leave a comment and we’ll get back to you as soon as we can.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
venkat
1 year ago

Hi

Good and very useful information about mkdir command. Every command option has detailed explanation.

You May Also Like
Bash Append to File
Read More

Bash Append to File

There are various ways to append text to a file in bash. In computing, append means to add…
Touch Command in Linux
Read More

Touch Command in Linux

Creating files is one of the most common things we do. Every now and then, you may also…