In this article we will discuss how to create a single or multiple directories in Linux using mkdir command. Then we will see various examples like, creating folders with space in name or creating folders with different permissions or creating nested directories recursively in single command.

Table of Contents

mkdir command – Introduction

Linux provides a command mkdir to create single or multiple directories,

mkdir <option> <directories names>

Options:

  • -m or –mode: Set the mode / permissions like chmod.
  • -p or –parents: Make parent directories if required and show no error in case directory exists.
  • -v or –verbose: Print a message for each of the created directory.

mkdir command creates the folder if it does not exists. If the folder already exists then based on the option provided, it will decide whether to show error or not. Let’s see some examples,

Create a single directory in Linux with mkdir command

To create a single directory in Linux, just provide the folder name along with mkdir command. For example,

ubuntu@Linux-VM:~/study$ mkdir project

It created a directory with name ‘project’. Let’s verify this,

ubuntu@Linux-VM:~/study$ ls
project

Create multiple directories in Linux with mkdir command

To create multiple directories in a single command provide all the names of directories to mkdir command, separated by space. For example,

ubuntu@Linux-VM:~/study/project$ mkdir src cfg lib

It created three directories ‘src’, ‘cfg’ and ‘lib’. Let’s verify this,

ubuntu@Linux-VM:~/study/project$ ls
cfg  lib  src

We can also use the verbose option i.e. -v while creating multiple directories. It will display the message after creating each directory. For example,

ubuntu@Linux-VM:~/study$ mkdir -v test1 test2 test3
mkdir: created directory 'test1'
mkdir: created directory 'test2'
mkdir: created directory 'test3'
ubuntu@Linux-VM:~/study$

What if we try to create a directory that already exist ? Will it show error or overwrite it ? Let’s know about this.

Linux: Create directory if not exists

If we use the mkdir command to create a directory that already exist, then by default mkdir will give error. For example,

ubuntu@Linux-VM:~/study/project$ ls
cfg  lib  src
ubuntu@Linux-VM:~/study/project$ mkdir src
mkdir: cannot create directory ‘src’: File exists

As ‘src’ directory already exists, therefore when we tried to create a new directory with same name using mkdir command (without any option) then it raised the error.

If you want a behavior that it should create a directory if it does not exist and in case directory already exists then just skip, without any creation.
Then we can do that using -p option. For example,

ubuntu@Linux-VM:~/study/project$ ls
cfg  lib  src
ubuntu@Linux-VM:~/study/project$ mkdir -p src
ubuntu@Linux-VM:~/study/project$

As ‘src’ directory already exists, therefore mkdir will not create new directory with same name. But as -p option was provided, therefore it didn’t gave an error. It just skipped the folder creation. So, when we use -p option flag with mkdir command then it will only create the directory if does not exist.

Linux: Create directory and subdirectories in one command – Recursively

If you create a folder and subfolders inside it in a single command then you need to provide -p option. Where -p stands for parents, it means if a complete folder path is given and any parent folder in the path is missing, then it will create that too. Let’s understand with some examples,

Let’s try to create a folder ‘source’ and inside the folder ‘source’ create a folder ‘code’ and inside that an another folder ‘test’. Nested folder structure should be like, ‘source/code/test’. If we try to create this nested folder structure with mkdir command without any option then it will give error,

ubuntu@Linux-VM:~/study$ mkdir source/code/test
mkdir: cannot create directory ‘source/code/test’: No such file or directory
ubuntu@Linux-VM:~/study$

By default mkdir command, can not create missing parent folders. To automatically create missing parent folders, use the -p option with mkdir. For example,

ubuntu@Linux-VM:~/study$ mkdir -p source/code/test

It created the completed nested folder structure recursively. Let’s verify this,

ubuntu@Linux-VM:~/study$ ls -R
.:
source
./source:
code
./source/code:
test
./source/code/test:
ubuntu@Linux-VM:~/study$

So, to create folder and subfolders in a single command in linux, use -p option with mkdir command.

Linux: Create directory with space in name

In Linux, creating a directory with space in name is little tricky, because if we directly provide a name with space to the mkdir command then it will create two directories instead of one. Let’s understand with an example,
Suppose we want to create a directory “test dir” using mkdir command. Let’s pass this name with space to mkdir command directly,

ubuntu@Linux-VM:~/study$ mkdir test dir

It will create two folders ‘dir’ & ‘test’. Let’s verify this,

ubuntu@Linux-VM:~/study$ ls
dir  test
ubuntu@Linux-VM:~/study$

This is not what we wanted. So, to create a folder with space in name we have two techniques,

Escape the spaces in name and pass to mkdir command

Escape the spaces in the name while passing it to the mkdir command. For example,

ubuntu@Linux-VM:~/study$ mkdir test\ dir

It created a folder with space in name. Let’s verify this,

Escape the spaces in name and pass to mkdir command

Encapsulate the folder name with spaces in quotes and pass it to mkdir command,

ubuntu@Linux-VM:~/study$ mkdir 'course work'

It created a folder with space in name. Let’s verify this,

ubuntu@Linux-VM:~/study$ ls
'course work'  'test dir'
ubuntu@Linux-VM:~/study$

Linux: Create directory with permissions

To create a directory with permissions we can use the -m option or -mode option. The syntax of permissions that we need to provide with -m option is similar to chmod. For example,

ubuntu@Linux-VM:~/study$ mkdir -m 777 test_dir

It will create a directory test_dir with permissions “rwxrwxrwx”. Let’s verify this,

ubuntu@Linux-VM:~/study$ ll
total 12
drwxrwxr-x 3 ubuntu ubuntu 4096 Dec 15 16:13 ./
drwxr-xr-x 6 ubuntu ubuntu 4096 Dec 15 03:39 ../
drwxrwxrwx 2 ubuntu ubuntu 4096 Dec 15 16:13 test_dir/

Linux: Create folder with date

To create a directory with mkdir command we need to use the command substitutions. For example,

Create a directory with today’s date

ubuntu@Linux-VM:~/study$ mkdir $(date +%Y-%m-%d)

It created a folder with today’s date. Let’s verify this,

ubuntu@Linux-VM:~/study$ ls
2020-12-15

Here the output of command $(date +%Y-%m-%d) is today’s date in YYYY-MM-DD format and we inserted that string to mkdir command, which in turns creates the folder with today’s date in format YYY-MM-DD.

Create a directory with today’s date and time

ubuntu@Linux-VM:~/study$ mkdir  $(date +%Y-%m-%d--%H-%M-%S)
ubuntu@Linux-VM:~/study$ ls
2020-12-15--16-22-15

Here the output of date command was passed to mkdir command and it in turn created a folder with today’s timestamp.

Summary

We learned how to create directories in Linux using mkdir command and we also explored all the options of mkdir command like created nested directories & setting permissions etc.