Multi-copying a file

Hello there,

I am writting a scrip with in shell (#!/bin/sh) and I need to copy 5 times the same file into different names:

 
cp xsec.1.11 xsec.1.12
cp xsec.1.11 xsec.1.13
cp xsec.1.11 xsec.1.14
cp xsec.1.11 xsec.1.15
cp xsec.1.11 xsec.1.16

and I have to do that for several file. That takes a lot a place in my script...so I was wondering if there is a way to copy a file in multiple places wit ha single command?

Thanks for your help.

Not directly. Use a loop, perhaps?

for f in 12 13 14 15 16; do
  cp xsec.1.11 xsec.1.$f
done

What's the use of the multiple copies? Perhaps you could put symbolic links in place once, and be done with it?

This will make five copies of xsec.1.11, five copies of xsec.2.11 and five copies of xsec.3.11. Hopefully this will be enough to get you started?

for file in xsec.1 xsec.2 xsec.3
do
  for num in 12 13 14 15 16
  do
    cp ${file}.11 ${file}.${num}
  done
done

Sing out if it's still confusing or not what you meant :slight_smile:

edit: oops, that'll teach me to open multipl threads at once then get distracted :slight_smile:

Thanks for your answers.

Unfortunately I had already though using a loop but I still needed as many lines to do the job...(If I had to make 1000 copies that would be definitely better, but for 5 or 6...).

In fact I wanted to make sure there was not a built-in function capable to do that! :slight_smile:

while read file
  do
   i=12
   while [ $i -lt 17 ]
    do
      cp -p "$file" /other/dir/"${file%.*}.$i"
      i=$(($i + 1))
    done
  done < files_list
$ cat files_list
xsec.1.11
xsec.2.11
xsec.3.11
...

Set the counter i (i=12) and the limit (17) to whatever value you desire.

I dunno if what I need is in the sort of same lines as this but I need to copy all *.txt files to a directory but change any reacurring names eg like multi readme.txt would have to rename as to keep all files

It might be better to open a new thread for your problem. Also post a clear sample of your input, expected output, and your efforts so far.