Trevor_zam Trevor_zam - 1 year ago 69
Bash Question

Duplicate folders and change a value in a file from that folder

I have a folder called main. I want to duplicate this folder along with all its files 300 times and rename each new folder as example0, example1, example2, example3 .... example300.

There are many files in the folder. I want to find the file config.ini, look for this line "amount = 0" each time, increment that 0 by 1 each time a new folder is created.

So example0 folder's config file amount value will be 0, example1 folder's config file amount value will be 1 and so on.

Can I get some help with this please. I would have just created the folders manually but 300 is a bit much. Looking for a simpler way. Thanks.

Answer Source
for i in {0..300} # for loop to iterate from 0 to 300
cp -R main "example$i"
sed -i "s/amount = 0/amount = $i/" "example$i/config.ini"

should do it.

What is happening here?

  1. The for-loop opens a counter i for you whose value sequentially go on from 0 to 300
  2. cp -R main "example$i" recursively copy main to example0, example1 and so on.
  3. sed command takes care substituting the string amount = 0 to amount = counter and the -i option makes the edit inplace.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download