How to copy one file into multiple subfolders at once on a Linux server?

3

3

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?

Matt V.

Posted 2011-09-28T17:08:07.930

Reputation: 165

Answers

5

for i in *.foo.bar.baz/; do
    cp file "$i"
done

user1686

Posted 2011-09-28T17:08:07.930

Reputation: 283 655

what if there is a file in the current dir that matches *.foo.bar.baz? – bryan – 2011-09-28T17:25:17.297

@bryan: See updated answer. (FWIW, ls -d would have listed files as well.) – user1686 – 2011-09-28T17:26:40.540

copied from the wrong terminal, added in my slash – bryan – 2011-09-28T17:31:04.213

2

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

nelaaro

Posted 2011-09-28T17:08:07.930

Reputation: 9 321

0

for dirname in $(ls -d *.foo.bar.baz/); do cp file $dirname; done

bryan

Posted 2011-09-28T17:08:07.930

Reputation: 7 848

2... no for in $(ls), please. Wildcard expansion already works for all commands. – user1686 – 2011-09-28T17:19:07.333