Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 4 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

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
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

You must log in to answer this question.

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