Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

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;"
share|improve this answer
    
i do not think the eval option works, since cp takes only one directory at a time as target –  1_CR May 2 at 23:33
1  
@1_CR Giving a try (with echo, of course) is often better than just thinking... –  Hauke Laging May 2 at 23:34
    
yes, actually works, sorry had used single quotes instead of double –  1_CR May 2 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. –  Hauke Laging May 2 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 –  1_CR May 3 at 0:02

with bash and xargs

printf "%s\n" /home/user/dl/{1..60}/111/23/ | xargs -n 1 cp script.sh 
share|improve this answer
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 at 16:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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