Creating Multiple Files with Bash

Published: 2022 February 16

system administration Linux Bash shell command

Introduction

When you want to create multiple files, Bash can help do it efficiently.

Below are several examples for creating files. To follow along, navigate to an empty folder where you want to create files and enter the commands.

A warning

You may wish to remove files after each step, but since the rm command is powerful and can delete more files than you intended without prompting, make sure you understand its use before blindly applying the example commands below.

If you’re not comfortable with that, use a GUI or another method you’re familiar with to remove the files.

Example Setup: Creating a test directory

Before we start creating and deleting files, you may wish to first create a new directory in which to test these commands.

This will make it easier to see the effects of the changes we’re making and will reduce the risk of impacting files we don’t want to alter.

Example 1: Creating a file

The touch command allows for the creation of an empty file.


To create a file called “file.txt” enter the following command.

Command

touch file.txt

Enter the command ls -1 and you should see the following result.

Result

file.txt

To delete the file enter the following command.

File deletion command (See the warning above before using this command.)

rm -i file.txt

Finally, enter the ls -l command to confirm the file was removed.

Example 2: Creating multiple files

To create several files at once you can use the brace expansion with a range.


Command

touch file{1..10}

Result

file1
file10
file2
file3
file4
file5
file6
file7
file8
file9

That will create 10 files, but without a filetype extension.


File deletion command (See the warning above before using this command.)

rm -i file*

Example 3: Creating multiple files with an extension

To create several files with an extension you only need to add the extension to the end of the command.


Command

touch file{1..10}.txt

Result

file10.txt
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt
file6.txt
file7.txt
file8.txt
file9.txt

File deletion command (See the warning above before using this command.)

rm -i file*.txt

Example 4: Creating multiple files with leading zeroes before the number

Another small adjustment will pad the number with leading zeroes.


Command

touch file{01..10}.txt

Result

file01.txt
file02.txt
file03.txt
file04.txt
file05.txt
file06.txt
file07.txt
file08.txt
file09.txt
file10.txt

File deletion command (See the warning above before using this command.)

rm -i file*.txt

Example 5: Creating multiple files with random alphanumeric strings

Now let’s add some random characters to differentiate the files.

We’ll use the shuf command that we previously developed to generate random alphanumeric strings.

Try the following:

shuf -erz -n $(shuf -e -n 1 {0..5})  {A..Z} {a..z} {0..9}; echo

Run the command multiple times to see different results.


Let’s first alter the command slightly to the form we’ll use in file creation and we’ll see a potential problem.

echo $(shuf -erz -n $(shuf -e -n 1 {0..5})  {A..Z} {a..z} {0..9})

Along with the output, you’ll see this warning:

bash: warning: command substitution: ignored null byte in input

The discussion of null bytes can be a bit involved, but the resolution can be quite simple.

To use this command in our file creation we’ll want to add the tr command to remove the null byte.

echo $(shuf -erz -n $(shuf -e -n 1 {0..5})  {A..Z} {a..z} {0..9} | tr -d '\0')

Now we can utilize the updated command to create files that contain random alphanumeric strings.


Command

touch file$(shuf -erz -n $(shuf -e -n 1 {0..5})  {A..Z} {a..z} {0..9} | tr -d '\0'){01..10}.txt

Result example

file07.txt
file6IJ04.txt
filec09.txt
fileE05.txt
filef03.txt
fileg06.txt
fileMv5W01.txt
fileOZV2i10.txt
files02.txt
fileXt08.txt

File deletion command (See the warning above before using this command.)

rm -i file*.txt

Example 6: Creating files with a specific ending

If we want to add a specific ending to a file name, that is trivial. We can add it in the same way we added the file extension in Example 3.

Let’s say we want to add “_final” to end of our files.


Command

touch file$(shuf -erz -n $(shuf -e -n 1 {0..5})  {A..Z} {a..z} {0..9} | tr -d '\0'){01..10}_final.txt

Result example

file01_final.txt
file02_final.txt
file04_final.txt
file1QS06_final.txt
file5u05_final.txt
file8l03_final.txt
fileG009_final.txt
fileIRtbH08_final.txt
filel07_final.txt
filezm310_final.txt

File deletion command (See the warning above before using this command.)

rm -i file*.txt

Copying Directory Structure With a Bash Script - With Help From ChatGPT AI

Published: 2024 January 22

Setting Up a Raspberry Pi as a Linux Print Server with CUPS

Published: 2024 January 19

Setting Up a Raspberry Pi Zero 2 W with Ubuntu Server

Published: 2024 January 04