How to make duplicates with different names from a single file?
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.
Related videos on Youtube
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, 2022Comments
-
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 over 11 yearsQuestion seems incomplete, just copying the files with different filename ?
-
Alan Wills -MSFT over 11 yearsyes correct @warl0ck
-
-
jokerdino over 11 yearsIt should. What happened when you tried?
-
knuckle_sandwich over 11 yearsHave you used
cd
to navigate to the directory? -
Alan Wills -MSFT over 11 yearsi 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 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 over 11 yearsI am probably doing it wrong but when I run your script, I get only
Helium,
file. What am I doing wrong? -
daisy over 11 years@jokerdino sorry, I made a mistake, now it works ;-P
-
daisy over 11 years@jokerdino I updated the script and generated result, maybe I'm wrong with your requirements
-
jokerdino over 11 yearsNever mind me. It actually is working.