I've used touch
for single creation of blank files and multiple blank files from a list using cat foo.txt | xargs touch
plus I ran across How to create multiple files with the Terminal? but I'm having issues figuring out how I can implement the creation of files that use a template from a list of files with pure bash.
File names in foo.txt file:
cpt-one.php
cpt-two.php
cpt-three.php
template.txt:
// lots of code
Is there a way I can use touch
to create the file's from the list using what's in template.txt instead of creating a blank file?
A
bash
-esque way to do it (without an explicit loop) might beread the filenames into a shell array variable
or its synonym
readarray -t files < foo.txt
expand the array inside a
tee
command[The
> /dev/null
is optional - it just hidestee
's default standard output from the terminal.]Unless you have thousands of files that you need to create, you can use brace expansion:
Since your objective is to clone template , use
tee
to redirect stdin to multiple files.tee
writes to both file and stdout, so you will see it echoed on the terminalAlternatively , one could use
while read
structure. with shell redirection.Note well, that this is a simple example, that assumes entries in the file contain no special chars
Now, since you actually want to copy single file in to multiple, you can use the same structure, to
cp /path/to/template "$FILE"