AWS, Bash

How to create files automatically with the bash script?

To-Do List: 7 Items Before Going Public Your Wordpress Site

In this article, learn how to create a simple bash script to automate the creation of n number of files. Before getting started, let’s define the bash script and IT automation.

What is the bash script? 

A bash script, or shell script, consists of a series of commands in a plain text file. We typically type these commands on the command line, for example, the commands ls and pwd. 

Any command executed using the command line can be put into a script, which will do the same. 

What is IT Automation 

Automation of IT systems is sometimes referred to as infrastructure automation, and it is the process of using software to create repeatable processes and instructions to reduce or eliminate human interaction with the systems. 

In this article, I will solve a particularly challenging task that was part of my AWS re/Start class. To be honest with you, it took me two days to solve the problem. I slightly modified the challenging part because of the AWS copyright matter.  

If you are an AWS re/Start student looking for a solution, you can figure it out by yourself at the end of this article.  

So here is my version of the challenge that is similar to the AWS re/Start challenge:  

The challenge is to create files automatically : 

  • Create five text files and add some text to each of these files 
  • The file name should be <anyName><number>, <anyName><number+1>, <anyName><number+2>  and so on 
  • Each time you execute the script, it creates the following five files with an increasing number, starting with the last or max number that already exists. 
  • You must not hard code these numbers. You must generate them using automation. 

Getting started 

Step -1: Create a Directory 

This AWS re/Start challenge’s first step is creating a directory. A directory is an isolated space in the UNIX environment where we store all the files. A file must be in the directory. If you do not wish to create any directory, a directory is already there, called the root directory.  So whatever file we create these files belongs to the directory.  

mkdir script
cd script

Step 2: Create Five text files and content 

We need to write a bash script to create five text files in this step. Before writing the script, we need to understand how we can do this job using the command line interface.  

What is the command for creating file in Linux: 

The touch command is a standard UNIX/Linux command used to create, modify, and change files. 

Touch file_name  

The exciting way to create files and add content is to use the echo command.  Many people may not know this technique to create and add content to a file. And that is the echo command.  

Echo “hello” > fileName

We will develop our script little by little. In your script directory, create a script file name auto_file.sh  

And add the following line of this code.

#!/bin/bash 
echo "Hello" > textFile_1.txt 

Make this file executable.  

chmod +x  auto_file.sh 
./auto_file.sh 

Now you should see the textFile_1.txt file in the script directory. We create only one file. What about creating file files? Yes, you guessed it. We need to use a loop.  We will use the for a loop. 

#!/bin/bash 

#Define the max number of file  

max_number=5 

count=0 

while [ $count -lt $max_number ] 

do  

`echo "Hello There I'm file number $count" > mdKhan_$count.txt`  

((count++)) 

done 

 Execute this command. You will see five files in your script directory. Let me explain the code: 

Line 3:  Define the maximum number of files.  In our case, it is 5. 

Line 4:  The loop needs to initial number and how many times the loop will be iterated. In this case, we started from zero.

Line 5: Checking the condition. If the count is less than the maximum number, then while looping, does the following statement 

Line 7:  Here, we use the echo command and concatenate the $coun to the text and with the file name. This is the main command that creates files. 

Line 8: We increase the count value by 1. count++ means count = count +1 

Once the count reaches the maximum value, the while loop will be done, creating the file. In this case, while loop creates five files.

At this point, each execution time, our script creates file files; it doesn’t create the following five files. That means our script is not working toward the challenges.  

We need to count the total file in the current directory to complete our challenge. We will count the total files in the script directory in this case. In the script directory, we have a total of six files, including our auto_file.sh.  

We can use a Linux command to count the total file in the directory. Here is it

ls -1 | wc -l 

This command outputs the total file. We will use the command on our bash script, and we will update the variable max_number

total_files=`ls -1 | wc -l` 

And we need to exclude the main script file. 

total_files="$(($total_files-1))" 

Before we assign the number 5 to the max_number. In this case, the maximum number should be the total files. 

We need to do this if we update the max_number with the total files.

max_number=”$(($total_files))” 

But there is one problem in this line of code. If someone first time executes this file, the $total_files will be 0 because we already excluded the script file.  Our script will not create any files. So how can we solve this problem?  

Let’s try updating the  max_number variable by adding an initial value of 5.  

max_number=”$(($total_files+5))” 

Now the initial max number is 5. So the first execution, our script will create the first five files. In the second execution, the max number will be 10.  

Here is the complete code. 

#!/bin/bash 

# Define the max number of file  

max_number=2 

count=0 


# Count and assined total number of files in the firectory 

total_files=`ls -1 | wc -l` 

 
# Ecxclude the script file from the total number of files 

total_files="$(($total_files-1))" 

 
# Update the max_number 
max_number="$(($total_files+2))" 

while [ $count != $max_number ] 

do  

`echo "Hello There I'm file number $count" > mdKhan_$count.txt`  

((count++)) 

done