5 examples to create multiple directories in Linux

5 examples to create multiple directories in Linux

Overview

Here we will show couple of ways/tips/hints on how to create multiple directories in Linux. The steps we want to cover in this article are how to create multiple directories in linux and sub-directories with the mkdir tool and for each individual example to create the directories with only one command. In other words, we want to run one mkdir command in terminal that will enables us to create multiple directories and sub-directories at once.

Just to recap first how we use mkdir to create a directory:

mkdir directory_name

This single command will of course create just one singe directory where are your currently positioned in your Linux system. As we mentioned where are now going to show examples how to create multiple directories and sub-directories. In these examples we’re also going to use the mkdir tool. It’s still powerful to do all the directory creation examples we’re going to show.

Example 1. Create a parent directory with sub-directories

This is an example where we need to create a single parent directory(or upper level directory) where inside of it, we need to create multiple sub-directories, all at one with one command. To do this, run the mkdir tool in a command like this:

mkdir -p main-directory/{subdir1,subdir2,subdir3,subdir4}

create multiple directories in Linux

When the mkdir tool is ran like this, this will allow us the create multiple directories at once for this case.This is because of the -p argument, which designates the first in line directory(our main directory) as a parent directory which then enables us to add arguments for creating sub-directories inside of the main directory.

Small note, there must not be any spaces between the sub-directory names, otherwise the command will not work.

Example 2. Creating multiple directories without a parent directory

If you just need to create multiple directories in  the in the currently positioned directory without having a main directory or creating a directory tree, we can just use brackets from example 1 and do just that:

mkdir {directory1,directory2,directory3}

create multiple directories in Linux

Example 3. Creating a full path directories(sub-directory inside of sub-directory)

What does this mean is, if we have, or need to create a directory path like the following:

directory1/directory2/directory3

we can also achieve this with the mkdir tool, Run the mkdir as following:

mkdir -p full-path/subdir1/subdir2

create multiple directories in Linux

Example 4. Bracket nesting

What we mean by this is – we can nest brackets from the example 1 and create multiple sub-directories inside sub-directories and all of them within the main directory.

mkdir -p another-directory/{subdir1/{subsub1,subsub2},subdir2/{subsub3,subsub4},subdir3,subdir4}

With a command like this, you can instantly create a directory tree.

create multiple directories in Linux at once with mkdir

Example 5. Creating directories and setting permissions at the same time

Mkdir tool also has options to set the permissions as you want(as you have assigned) and create a new directory at the same time.

mkdir –m 777 anotherdirectory

You can also combine this option with other examples we showed earlier and use to set a folder permission at once on multiple directories.

For an example, we’re now going to create a parent directory with multple sub-directories and have them all the same permissions:

mkdir -m 777 -p permissions/{one,two,three}

create multiple directories in Linux at once with mkdir

Summary

We showed 5 different examples or use cases on how to create multiple directories in linux with the mkdir tool by running a single command in terminal. Mkdir is a powerful tool for creating directories and it has many more options for directory creation(which can be seen in the mkdir man page). The beauty of this tool that it’s options can be combined and the examples above we showed can also be combined to create directory trees with even more complexity or for many more use cases. it is highly suggested to go through the mkdir man page(usage documenation or manual). Link to the man page – mkdirmanpage.

Thank you very much for your time…

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *