How to make duplicates with different names from a single file?

6,643

Solution 1

You would like to copy out a list of files from same source:

#!/bin/bash
Elements=('Helium' 'Aluminium' 'Argon') # Items to copy
Source='Hydrogen'  # Copy from this file

for x in "${Elements[@]}"; do 
  cp "$Source" "$x.element"
done

After which I will get:

Aluminium.element Argon.element Helium.element

They all share the same content of Hydrogen

Solution 2

Why not use cp command?

for ELEMENT in {1..10}.element; do cp Hydrogen.element element$ELEMENT; done

This command would create 10 files of the name element1.element, element2.element, etc. You can then just rename the files with the relevant element name. If you need to generate more element files, you can change the {1..10} to {1..92} to create 92 files.

(reference)

Share:
6,643

Related videos on Youtube

Alan Wills -MSFT
Author by

Alan Wills -MSFT

I am a intermediate programmer and a user. I like playing sports. I am a student at IIT Kharagpur who has a thing for Mathematics.

Updated on September 18, 2022

Comments

  • Alan Wills -MSFT
    Alan Wills -MSFT over 1 year

    I want to duplicate the contents of a file. Suppose there is a file named "Hydrogen.element". I want to duplicate the contents of this file with a different name ie.make another file named "Helium.element". I am actually making a periodic table and I want to use the file named "Hydrogen.element" as a template.

    • daisy
      daisy over 11 years
      Question seems incomplete, just copying the files with different filename ?
    • Alan Wills -MSFT
      Alan Wills -MSFT over 11 years
      yes correct @warl0ck
  • jokerdino
    jokerdino over 11 years
    It should. What happened when you tried?
  • knuckle_sandwich
    knuckle_sandwich over 11 years
    Have you used cd to navigate to the directory?
  • Alan Wills -MSFT
    Alan Wills -MSFT over 11 years
    i tried it with multiple file like cp Hydrogen.element Helium.element Aluminium.element Argon.element Arsenic.element Antimony.element Astatine.element Acitnium.element Americium.element
  • jokerdino
    jokerdino over 11 years
    @PranitBauva The given command would work for only element. I'll update my answer in a while with your updated requirement.
  • jokerdino
    jokerdino over 11 years
    I am probably doing it wrong but when I run your script, I get only Helium, file. What am I doing wrong?
  • daisy
    daisy over 11 years
    @jokerdino sorry, I made a mistake, now it works ;-P
  • daisy
    daisy over 11 years
    @jokerdino I updated the script and generated result, maybe I'm wrong with your requirements
  • jokerdino
    jokerdino over 11 years
    Never mind me. It actually is working.