Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: January 3, 2024
File creation is one of the fundamental aspects of Linux systems and plays a pivotal role in the overall functionality and organization of the operating environment. In Linux, file creation is about establishing a structured and systematic approach to storing and retrieving information.
The ability to create multiple files with specific properties, such as prefixes and extensions, can be helpful in various scenarios:
In this tutorial, we’ll learn how to create multiple files with specific prefixes and extensions using the Linux CLI.
The code in this tutorial underwent testing on a Debian 12 (Bookworm) system using GNU Bash 5.1.16.
We can use POSIX commands like printf, touch, and brace expansion to create multiple files having specific prefixes and extensions.
In general, we can create files using the touch command.
Let’s combine the touch command with brace expansion to create multiple files:
$ touch step_{1..10}.txt
This command creates ten files with names step_1.txt to step_10.txt in the current working directory. Each file has the prefix step_ and the extension .txt.
Additionally, we can use the brace expansion to create specific files by going over each element in the provided sequence:
$ touch part_{header,body,footer}.csv
Here, we create three different files: part_header.csv, part_body.csv, and part_footer.csv, in the current working directory. In this command, we used part_ as the prefix and .csv as the extension for the files we create.
We can use the printf command to create a list of strings that serves as an argument list for the subsequent command. With the help of extended arguments (xargs), we can pass each element from the argument list to the touch command:
$ printf step_'%s'.txt' ' {A..F} | xargs touch
The above command can be divided into two parts: the part preceding the pipe (|) and the part following the pipe.
The first part preceding the pipe (|) generates an argument list with file names having step_ as the prefix and .txt as the extension. The second part following the pipe processes each element from the previously generated argument list to create files with the provided names.
Let’s break down this command to understand each option it employs:
Overall, this command creates six files with names step_A.txt to step_F.txt. All the files share a common prefix step_ and a common extension .txt.
Bash provides concise ways to express loops, making it efficient for quick tasks. We can create multiple files with specific prefixes and extensions using shell loops: for and while.
The for loop is a control flow statement commonly used to repeatedly execute code based on a specified sequence or range of values. We can specify the number of iterations using the control expression:
$ for ((i=5; i<=10; i++)); do >> "file_$i.csv"; done
This command results in six files with names file_5.csv to file_10.csv. In this case, we used file_ as the prefix and .csv as the extension for the files created.
Let’s understand each option used in this command:
Moreover, we can also use brace expansion to define the control expression in the for loop:
$ for i in {5..10}; do >> "file_$i.csv"; done
Both of the above commands are the same and accomplish identical goals resulting in six files with the same naming convention.
The while loop is another control flow statement used to execute a block of code repeatedly as long as a specified condition evaluates to true.
Let’s create multiple files with newdata_ as the prefix and .txt as the extension using the while loop:
$ i=1; while [ "$i" -le 9 ]; do >> "newdata_$i.txt"; i=$(($i + 1)); done
The above command creates nine files with names newdata_1.txt to newdata_9.txt.
Let’s take a closer look at the new option used in this command:
At the end of this while loop, the value of $i should be 10, i.e., greater than 9. So, let’s check the value of $i:
$ echo $i
10
We can use the redirection operator (>) instead of the append redirection operator (>>) in the above commands to overwrite the existing files, if any.
Additionally, we can use the string comparison operators in Bash to compare two strings for evaluating conditions. There are two string comparison operators in Bash: string equal (=) and string not equal (!=).
The task of creating multiple files with specific prefixes and extensions can be accomplished using various scripting and programming interpreters. For simplicity, we’ll limit ourselves to only a few programming languages in this tutorial.
In Perl, we can construct a similar for loop to the one demonstrated in Bash earlier:
$ perl -e 'for ($i=1;$i<=10;$i++) { open($f, ">", "file_$i.pl"); }'
This perl command creates 10 files with the prefix file_ and the extension .pl.
The perl -e construct invokes perl for one-liners directly from the command line. The open() system call creates the files with specified names, in this case, file_$i.pl where $i acts as a placeholder for a numerical variable that takes values from the loop iteration.
Python also has the for loop command to execute a code block repeatedly:
$ python -c 'for i in range(2,8): open("file_"+str(i)+".py", "w");'
This command creates six files with the names file_2.py to file_7.py. In this example, we used file_ as the prefix and .py as the extension to create multiple files.
The python -c invokes the Python interpreter in one-liner mode, for i in range(2,8) is a loop iterating over the range from 2 to 7, and open() opens a file with the provided name in the write-only mode.
The range() function in Python can work with the upper limit alone. For example, range(8) iterates the loop from 0 to 7.
Ruby has a different syntax for iteration:
$ ruby -e '(1..10).each { |i| File.open("file_"+i.to_s+".rb", "w") }'
This one-liner creates 10 files in the current directory having the prefix file_ and the extension .rb.
The (1..10).each from the above command creates a range from 1 to 10 and iterates over each element in the range. i.to_s in the file naming convention converts the loop variable i to a string.
In this article, we explored several ways to create multiple files with specific prefixes and extensions. Of course, we can select any method depending on our preferences.
First, we discussed the usage of POSIX-standard tooling and shell loops to create multiple files with specific prefixes and extensions. Then, we practiced two different versions of the control expression in the Bash for loop.
Finally, we used the one-liners from higher-level programming languages to achieve the same goal of creating multiple files with specific prefixes and extensions.