Creating a new directory using mkdir - Bash

This article covers how to use the bash command mkdir.

The mkdir command allows you to make a new directory.

Fortunately, there aren't many options, making it very easy to use.

Making a new directory

Simply add the name of the new directory after the command:

mkdir <new-directory-name>

For example:

mkdir MyFiles

Making multiple directories at once

You can add multiple directory names after the command to create multiple directories at once.

mkdir <new-directory-name-1> <new-directory-name-2> ...

For instance:

mkdir MyFiles MyMusic MyCode

Making nested directories

What if you wanted to create a nested directory? You could change directory each time with the cd command:

# Create the nested directory "MyFiles/FamilyPhotos/2021"
mkdir MyFiles
cd MyFiles
mkdir FamilyPhotos
cd FamilyPhotos
mkdir 2021

However, this is cumbersome. Thankfully, we can just supply the path directly to the command, using the -p parameter.

If one of the subdirectories doesn't exist, it will create it automatically:

mkdir -p MyFiles/FamilyPhotos/2021