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

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

Or, more fun:

eval "cp script.sh /home/user/dl/"{1..60}"/111/23;"
  • i do not think the eval option works, since cp takes only one directory at a time as target
    – iruvar
    May 2, 2014 at 23:33
  • 1
    @1_CR Giving a try (with echo, of course) is often better than just thinking... May 2, 2014 at 23:34
  • yes, actually works, sorry had used single quotes instead of double
    – iruvar
    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. 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
    May 3, 2014 at 0:02

with bash and xargs

printf "%s\n" /home/user/dl/{1..60}/111/23/ | xargs -n 1 cp script.sh 
  • 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
    May 3, 2014 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.