Bash: How to Create Multiple Files at Once


Often you may want to use Bash to create multiple files at once.

You can use the following methods to do so:

Method 1: Create Multiple Empty Files at Once

for i in {1..5}; do touch my_file${i}.txt; done

This particular example will create five text files in the current directory with the filenames my_file1.txt, my_file2.txt, etc.

Method 2: Create Multiple Files with Content at Once

for i in {1..5}; do echo "hey there" > "my_file${i}.txt"; done

This particular example will create five text files in the current directory with the filenames my_file1.txt, my_file2.txt, etc. and contain the contents “hey there” in the first line of the file.

The following examples show how to use each of these methods in practice.

Example 1: Create Multiple Empty Files at Once in Bash

Suppose that we would like to create five empty text files in our current directory.

We can use the following syntax to do so:

for i in {1..5}; do touch my_file${i}.txt; done

The following screenshot shows how to use this syntax in practice:

Bash create multiple empty files at once

Notice that when we use the ls statement to view the files in our current directory we can see that the following five text files were created:

  • my_file1.txt
  • my_file2.txt
  • my_file3.txt
  • my_file4.txt
  • my_file5.txt

Feel free to change the range of values specified in the curly brackets to create a different number of files.

Also feel free to change the .txt extension to a different extension if you’d like to create a different type of file.

Example 2: Create Multiple Files with Content at Once in Bash

Suppose that we would like to create five text files in our current directory that each contain the text “hey there” in the first line of the file.

We can use the following syntax to do so:

for i in {1..5}; do echo "hey there" > "my_file${i}.txt"; done

The following screenshot shows how to use this syntax in practice:

Bash create multiple files with content

Notice that when we use the ls statement to view the files in our current directory we can see that five text files were created.

We can also use the cat command to view the contents of one of the files:

We can see that the file contains the text “hey there” on the first line of the file, just as we specified.

Feel free to use similar syntax to create as many files as you’d like with content in your own directory.

Related Tutorials

The following tutorials explain how to perform other common tasks in Bash:

Bash: How to Add Files Extension to All Files
Bash: How to List All Files in Folder Without Extension
Bash: How to Rename Extension of Multiple Files

Leave a Reply