5

I have a file that I want to copy to another 60 directories.

The best way I can think of doing this is by making a bash script that has instructions to every folder like this:

cp script.sh /home/user/dl/*/111/23
cp script.sh /home/user/dl/*/111/23
cp script.sh /home/user/dl/*/111/23
and so on...

The * is a number starting at 1 and finishing at 60

There has to be a better way than this.

2 Answers 2

8
for((i=1;i<61;i++)); do
  cp -p script.sh /home/user/dl/${i}/111/23
done

Or, more fun:

eval "cp script.sh /home/user/dl/"{1..60}"/111/23;"
5
  • i do not think the eval option works, since cp takes only one directory at a time as target
    – iruvar
    Commented May 2, 2014 at 23:33
  • 1
    @1_CR Giving a try (with echo, of course) is often better than just thinking... Commented May 2, 2014 at 23:34
  • yes, actually works, sorry had used single quotes instead of double
    – iruvar
    Commented May 2, 2014 at 23:38
  • @1_CR Doesn't make a difference, does it? There's nothing to expand in the quoted string. The mistake must have been something different. Commented May 2, 2014 at 23:39
  • I had used something like this eval "cp script.sh "'dl/{1..10}'"/23/;". Got lazy and modified a previous example i was trying out instead of copying yours verbatim
    – iruvar
    Commented May 3, 2014 at 0:02
5

with bash and xargs

printf "%s\n" /home/user/dl/{1..60}/111/23/ | xargs -n 1 cp script.sh 
1
  • 1
    What's more, you could use xargs' -P option to execute in parallel, which could give a good speed-up depending on how the target directories are distributed on the underlying filesystem(s).
    – pimlottc
    Commented May 3, 2014 at 16:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .