Linux

How to create multiple directories at once on Linux (easy and fast)

September 25, 2021

How to create multiple directories at once on Linux (easy and fast)

We know that with mkdir we can create a directory, in this article I will show you how to create multiple directories at once on Linux, with one command!

create multiple directories at once on Linux cover

How to create multiple directories in one command

The mkdir command is used to create directories, however, in its normal form, it only creates one at a time.

Look:

mkdir directory

The problem is when we try to use it to create several directories at once, a folder structure, for example:

mkdir dir1/dir2/dir3

In this case, we receive the following message:

mkdir: could not create directory “dir1/dir2/dir3”: File or directory does not exist

Because it tries to create dir3 inside the dir2 folder that is inside dir1.

That is, it creates only one directory, dir3, this is the normal behavior of the command, it’s not wrong.

So we must add the -p flag to the command, then Linux will correctly interpret our action, see:

mkdir -p dir1/dir2/dir3

In this way, the dir1/dir2/dir3 structure is created in our system.

Creating multiple directories but no structure

There is also the possibility to create multiple directories, but without structure.

In this case, the mkdir command by itself already meets the demand, see:

This way you will create 3 independent directories: dir4, dir5, and dir6

Conclusion

With these two ways to create folders, it is already possible to meet 99.9% of the demands, and we solve the main question: how to create multiple directories at once on Linux?

Remember that for structures use the -p flag/parameter in mkdir.

And for separate directories just space the folders with the space and Linux will take care of creating them.

And that’s it for today, until the next post.

Want more content about Linux? Click here!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x