How to Create Multiple Files and Directories at Once in a Linux Terminal

Create multiple files and directories in Linux with just a single command, which will save you from typing for a long time.

If you are using Linux, then you know how to use the mkdir command to create a directory, and for files, we use the touch command, which creates an empty file in a second without using any command line editor.

Both commands are handy to use, but how do I use this command to create multiple files and directories at once in Linux? Because it’s easy to create multiple files of 10 directories or files, but if someone asks you to create 100 files, you cannot easily do it until you find this article.

In this article, you will learn how to create multiple directories and files with just a single command, which will save you from typing for a long time.

Creating Multiple Files at Once

First, we will see how to create multiple files at once by using the touch command, and after that, you will see how to create multiple files using the “for loop” function.

Creating Multiple Files at Once using touch Command

When you have decided to create multiple files on your Linux system, all you need is the touch command that you will definitely find on your system, and the last thing you should know is how you want to name them.

After that, you just need to follow the below syntax to create multiple files at once by just invoking the single command.

$ touch filename{1..N}

The syntax is pretty simple; you just need to mention the file name which you want to create, and with the help of Brace expansion, set the starting number and end number in curly braces.  

To explain it to you more convincingly, let me first create a directory where we can learn how to use the above command and even suggest you do the same because running the above command will create multiple files, which can mess up your current directory structure.

$ mkdir createmultiplefile
$ cd createmultiplefile

After getting into the ‘createmultiplefile’ directory, execute the below command, which will create a total of 10 regular files in the current directory.

$ touch filename{1..10}

When you run ls --sort=none, you will find the above command has performed exactly as we wanted.

The output of the above command:

Show multiple file create using touch command
Show multiple files create using the touch command

With a minor change, you can create 100, 1000, or 10,000 files in a matter of seconds.

$ touch filename{1..100}
$ touch filename{1..1000}
$ touch filename{1..10000}

This will not be the same case if you try to create 500K (5 lakhs) files at once, because commands like touch, mv, rm, and other commands have a certain limitation of accepting arguments as per the available stack size.

In this case, you can surpass the argument limit by using “for loop” to create multiple files, but there is one drawback to using that command it takes a long time to complete.

$ for i in {1..1000000}; do touch filename$i.txt; done

Creating Multiple Text Files with (.txt) Extension at Once using the touch Command

Above, you learned how to create multiple files, which are fine to use if you just want to dump regular files.

On the other hand, if you want to create a file with a specific extension, then you need to just append the extension name at the end of the file.

For example, if you want to create 10 text files (.txt) at once in the current directory, then you should invoke the below code, which will create 10 different text files with .txt extensions.

$ touch filename{1..10}.txt

You can run the same code if you want to create .html, .js, .php, .py, or any other extension, but don’t forget to replace them with the appropriate extensions.

$ touch filename{1..10}.html
$ touch filename{1..10}.js
$ touch filename{1..10}.php
$ touch filename{1..10}.py

Creating Multiple Directories

After using the above command, we know how we can use the bash expansion to create multiple files, but we didn’t show you how you can create multiple directories.

So let’s get to that topic.

Creating Multiple Directories at Once Using the mkdir Command

Conventionally, when you want to create multiple directories, you may be using the below command, which is totally inefficient if you want to create 100+ directories at once.

$ mkdir dir1, dir2, dir3

The most efficient way to create multiple directories is to execute the below code, which will create a number of specified directories in the blink of an eye.

$ mkdir directoryName{1..100}

The output of the above command:

Create multiple directories
Create multiple directories

Create multiple Directories within a subdirectory

If you want to create multiple subdirectories within the parent directory, then you can use the below code, which will create a total of 25 directories, and each parent directory will contain 10 sub-directories.

$ mkdir -p parentdirectory{1..25}/subdirectory{1..10}

Even you can try this:

$ mkdir -p parentdirectory{1..10}/{dirA,dirB,dirC}

Creating Multiple Directories in Hierarchal Using the mkdir Command

When you want to create multiple directories in hierarchical order, then you should use the below code, which will create a directory within a directory in hierarchical order.

$ mkdir -p par-dir/sub-dir/{sub-dir1,sub-dir2}

For example, if I want to create a project directory structure for the Flutter application, it should be like the below image.

Create multiple directories in hierarchical
Create multiple directories in hierarchical

Then I can use the below code, which will create the first lib/ directory within that src/ under src/ features/, and after that, features will hold a presentation, application, domain, and data.

$ mkdir -p lib/src/features/features{presentation,application,domain,data}

Wrap up

That’s all for now!

In this article, you have learned how to create multiple directories and files using the mkdir and touch commands in Linux with the brace expansion method.

If you know of any other way, then do share it with us in the comment area.

This Post Has 2 Comments

  1. Vikas

    I am learning linux from other website and had completed all about “touch” and “mkdir” command but no where it taught me about creating multiple files or directories on a single command. Your content is beautifully written and explained nicely. Thanks for sharing.

    1. Gagan Mishra

      Thanks for your kind word.

Leave a Reply