LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-05-2002, 06:03 AM   #1
carpman
Member
 
Registered: Jan 2002
Posts: 54

Rep: Reputation: 15
copy 1 file to multiple dir script


Hello, i have a server that has hundreds of domains that are use for email, the actual sites are just used to point users to main site and act as search engine boosters.

Obviously i do not want to changed each file one by one, and though i am new to linux i know that i can create a shell script to copy the new index.html to all the individual /home/domain/public_html/ directories.

Can any one help me out with how best to acheive this?


many thanks
 
Old 02-05-2002, 06:42 AM   #2
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
for dirname in domain1 domain2 domain3
do
cp index.html /home/${dirname}/public_html
done

Or you could do something like

for dirname in `ls /home/domain*`
do
cp index.html /home/${dirname}/public_html
done

Last edited by Mik; 02-05-2002 at 06:47 AM.
 
Old 02-05-2002, 06:16 PM   #3
carpman
Member
 
Registered: Jan 2002
Posts: 54

Original Poster
Rep: Reputation: 15
thanks for reply, this looks good if i want to changed index.html file in all

/home/domain/public_html/

which i don't, it need it to be selective

somthing like

cp index.html /home/${domain1,domain2,domain,7,domain15}/public_html
done
 
Old 02-05-2002, 06:34 PM   #4
Steave
Member
 
Registered: Jul 2001
Location: Braunschweig, Germany
Distribution: Suse 7.2
Posts: 184

Rep: Reputation: 30
Just as Mik said:

for dirname in domain1 domain2 domain15
do
cp index.html /home/${dirname}/public_html/
done
 
Old 02-06-2002, 12:46 PM   #5
carpman
Member
 
Registered: Jan 2002
Posts: 54

Original Poster
Rep: Reputation: 15
eithir i am slow to undestand or you do not understand what i want to do.

The syntax given seems to be to copy new index.html to one domain at a time or all domains at same time.


I want to automatically copy new index.html file to a list of selected domains, not all my domains.

This why i asked how to incluede list of domains 'user name' dir
In below how would i insert a list of domain user dir, where
{list} is where the list would go.

cp index.html /home/${list}/public_html
done
 
Old 02-06-2002, 12:53 PM   #6
Steave
Member
 
Registered: Jul 2001
Location: Braunschweig, Germany
Distribution: Suse 7.2
Posts: 184

Rep: Reputation: 30
Exclamation

An another way:
it's

for domain in domainx domainxy domaintotallydifferent
do
cp index.html /home/${domain}/public_html
done

Note that its totally up to you which domain-names you choose in your "in ..." List.

you might also do something like

for domain in domain1 steave domain17 www
do ...

to have the index.html copied to the public_html dir of the "users"
domain1, steave, domain17 and www

Steave.

Last edited by Steave; 02-06-2002 at 12:54 PM.
 
Old 02-06-2002, 02:31 PM   #7
carpman
Member
 
Registered: Jan 2002
Posts: 54

Original Poster
Rep: Reputation: 15
Hello, thanks for help but i am really confused now


Ok i have 10 domains and i wish to change the index.html in 5 of them. The dir structure is like this

/home/domain1/public_html/
/home/domain2/public_html/
/home/domain3/public_html/
/home/domain4/public_html/
/home/domain5/public_html/
/home/domain6/public_html/
/home/domain7/public_html/
etc

i create a shell script with syntax

cp index.html /home/${domain1 domain2 domain3 domain4 domain5 }/public_html
done


would this work ?

thanks for help
 
Old 02-06-2002, 03:13 PM   #8
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
what kind of syntax do you mean?

what you said, "cp index.html /home/${domain1 domain2 domain3 domain4 domain5 }/public_html"

is completely wrong (no offense!), bash can't do that, and specifying a program to run like that would not work, as you'd be using 'cd' not your own script by the way you wrote that, you have been given pretty much the same solutio tweice now, whcih does exactly what you are after. what else do you require that their solutions don't provide?

personally i'd use a shift call to move the script parameters along, and call $2 continuously in a for loop, but there's loads of ways of doing the same thing.

you seem to be being somewhat dogged in this illegal syntax you seem to be after. I'd suggest reading up on shell scripting. look at some of the books on the link on my signature
 
Old 02-06-2002, 06:17 PM   #9
Steave
Member
 
Registered: Jul 2001
Location: Braunschweig, Germany
Distribution: Suse 7.2
Posts: 184

Rep: Reputation: 30
OK. Probably some explanation here:

for dirname in astring anotherstring somestring
do
- code -
done

lets $dirname be (in the order stated) all of the strings in the list followin the "in"

so inside the do-done segment, you can use $dirname to -in your case copy- stuff to the desired directories.

Got it now?
 
Old 02-07-2002, 04:36 AM   #10
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
Very very confusing. To confuse you even more try this:

export list="domain1 domain2 domain5"

for dirname in ${list}
do
cp index.html /home/${dirname}/public_html
done


Still the same thing but maybe it makes it clearer for you.
 
Old 02-07-2002, 04:45 AM   #11
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
filename=$1
shift
for dirname in $*
do
cp ${filename}/home/${dirname}/public_html
done

removes the need to set a variable. just state all names as parameters
 
Old 02-07-2002, 06:12 AM   #12
carpman
Member
 
Registered: Jan 2002
Posts: 54

Original Poster
Rep: Reputation: 15
Ok i think you lot are winding me up

let me see if i have got it,

if i create a file called copy-index.txt with the following syntax in it and set permission to execute it will do the required task.

syntax in file copy-index.txt =



filename=$index.html
shift
for dirname in $domain1 domain 2 domain3
do
cp ${filename}/home/${dirname}/public_html
done


many thanks guys
 
Old 02-07-2002, 06:43 AM   #13
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
nah, you see now that's a general jumble of al the different solutions... you really need to read up on shell scripting. the way i've done it here is a little more subtle i think than most other solutions, and is more flexible. i'll let you fine out exactly what shift and $* actaully mean.. but this DOES work...

[chris@trevor chris]$ cat script
filename=$1
shift
for dirname in $*
do
echo cp $filename /home/$dirname/public_html
done
[chris@trevor chris]$ ./script a b c d e
cp a /home/b/public_html
cp a /home/c/public_html
cp a /home/d/public_html
cp a /home/e/public_html

yeah? obviously, just remove the word 'echo' and it'll go copy the stuff, rather than just pritning out the command like i've done there.

and why do you want to call it blah.txt? it's not a text file, it's a script file. generally you wouldn't ever give script files a suffix at all.

Last edited by acid_kewpie; 02-07-2002 at 06:44 AM.
 
Old 02-07-2002, 07:06 AM   #14
carpman
Member
 
Registered: Jan 2002
Posts: 54

Original Poster
Rep: Reputation: 15
thanks i will give it a try after i do a little more reading on subject.
 
Old 02-07-2002, 09:29 AM   #15
DavidPhillips
LQ Guru
 
Registered: Jun 2001
Location: South Alabama
Distribution: Fedora / RedHat / SuSE
Posts: 7,163

Rep: Reputation: 58
what your not understanding is this


the word for sets a variable


for variable in something anything somethingelse


now all of the words values to the right of in will be assigned to $variable one at a time in the loop.

since there are three words the loop will run three times.

1st run

do

echo $variable

is the same as

echo something

done

2nd run

do

echo $variable

is the same as

echo anything

done

3rd run

do

echo $variable
is the same as

echo somethingelse

done



that being said, the shift is better

Last edited by DavidPhillips; 02-08-2002 at 04:56 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to process every file in current dir and sub dirs BuckRogers01 Linux - Software 3 09-06-2005 07:32 AM
scp/ssh tail(multiple file) remote copy tpreitano Linux - General 1 08-22-2005 02:17 PM
Copy one file to multiple directories cmfarley19 Linux - General 9 11-10-2004 12:57 PM
Multiple file copy james.farrow Linux - Newbie 8 08-17-2004 08:01 AM
copy multiple files into one file HelpPlease Programming 2 12-09-2003 02:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration