Looping Through Files

I needed a fast way to update the ‘additional Nginx directives’ in a Plesk server, the problem is that creating a service plan and then updating by moving them into the new service plan does not work constantly. Everytime I ran it the nginx directives did not change. The file that I needed to update is located here


By using some basic BASH, I created the following code that updated all the Nginx files together. I put the list of domains which I got by using ls > filelist.txt inside the vhosts directory, then built the following code to solve my problem. Obviously, be careful if you’re doing this, I did a couple of test runs before I executed these commands.

# read through the filelist and echo each files code
for file in $(cat filelist.txt); do echo $file; done

# this command will empty all the files listed in filelist.txt, this is done to prepare all my files for the correct code to be inserted
for file in $(cat filelist.txt); do truncate -s 0 $file; done

# this command will read/loop through the filelist line by line. For each line in the file it 'cat' the output of nginx.txt and insert it into the file
for file in $(cat filelist.txt); do cat nginx.txt >> $file; done