Doing a copy of a file to sub-dirs

Here is a directory structure

a
b
c
d
e
f
g


all of the above are directorties

I want to copy file file.txt to each of those directories

i thought cp file.txt */  would do it but that did not work.


any ideas?

Thanks!

Randy
LVL 6
rjohnsonjrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

owensleftfootCommented:
#!/bin/bash

for directory in a b c d e f g
 do
cp file.txt $directory/
 done
0
rugdogCommented:
ls -d a b c d e f g|xargs -n1 cp file.txt
0
wesly_chenCommented:
Hi,

  For rugdog's script/comand, I did some modification:
----
ls -d * |xargs -n1 cp file.txt
----
Wesly
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

rugdogCommented:
Wesly,
   that's dangerous assuming file.txt, a, b, c, d, e, f and g are not the only files in the current directory. if you have more files all will end being like file.txt.
0
TintinCommented:
If you don't want to type in the directories and/or they will vary in name and quantity, do:

find /path/to/dirs -type d -maxdepth 1 -exec cp /path/to/file.txt {} \;
0
rjohnsonjrAuthor Commented:
TinTin,

Please describe your command so I can understand what each part does.

Thanks!

Randy
0
badrulnmCommented:
based on rugdog command, if you want to copy the file to all subdirectories:

# ls -F |grep "/" |xargs -n1 cp file.txt
0
rugdogCommented:
a simplified version of tintin's and badrulnm's and safer version of wesly's:

ls -d */|xargs -n1 cp file.txt
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ITG-SSNACommented:
I have learned to use perl from command line for times like this. I liken it to "Using the force"

Assuming you are in the parent directory of a->g:

perl -e 'my @arry=(a..g); for (@arry) {system("cp file.txt ./$_");}'

Highest regards,

~K Black
Irvine, Ca.
0
TintinCommented:
rugdog's version is certainly the shortest and simpliest.

To expand of the find command:

find /path/to/dirs -type d -maxdepth 1 -exec cp /path/to/file.txt {} \;

/path/to/dirs - Path to the directory to start searching from
-type d  - Only match directories
-maxdepth 1 - Don't descend to subdirectories
-exec  fork a command
cp /path/to/file.txt {} \;   - Copy file.txt to the matched directory, which is specified with {}
0
badrulnmCommented:
tintin, i found that your find command statement will also copy file.txt to the directory i start searching from.
0
TintinCommented:
I can't possibly see how you would get two different versions of file.txt being copied.  Are you using correct paths?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.