Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a folder on a Linux server with 35+ subdirectories, along the lines of:

aa.foo.bar.baz
ab.foo.bar.baz
..
bp.foo.bar.baz

I have a file that I want to copy into each of those subdirectories. What's a quick way to do that without running 35+ separate cp commands manually?

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted
for i in *.foo.bar.baz/; do
    cp file "$i"
done
share|improve this answer
    
what if there is a file in the current dir that matches *.foo.bar.baz? –  bryan Sep 28 '11 at 17:25
    
@bryan: See updated answer. (FWIW, ls -d would have listed files as well.) –  grawity Sep 28 '11 at 17:26
    
copied from the wrong terminal, added in my slash –  bryan Sep 28 '11 at 17:31
    
That worked great, thanks! –  Matt V. Sep 28 '11 at 17:44
add comment

find . -type d | xargs -I{} cp ./myfile {}/

This is how I got the job done.

Here was the problem I wanted solved. I wanted to test all my new virtual hosts before installing the actual applications.

pwd 
/var/www/
find . -type d
.
./site1
./site2
./site3
find . -type d | xargs -I{} cp ./php.info {}/ 
ls ./*
./php.info

./site1:
php.info

./site2:
php.info

./site3:
php.info

share|improve this answer
add comment
for dirname in $(ls -d *.foo.bar.baz/); do cp file $dirname; done
share|improve this answer
2  
... no for in $(ls), please. Wildcard expansion already works for all commands. –  grawity Sep 28 '11 at 17:19
add comment

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.