LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Copy single file to multiple locations - What am I doing wrong (https://www.linuxquestions.org/questions/linux-newbie-8/copy-single-file-to-multiple-locations-what-am-i-doing-wrong-632667/)

decartes 04-03-2008 06:05 AM

Copy single file to multiple locations - What am I doing wrong
 
Hi

I have a little server running qmail/plesk . The problem is my ISP wont support Spamassassin due to load issues on the host server and I cannot afford to migrate to a root server (using VMWARE at the mo).

I have spamassassin installed manually but also need to use plesk for adding domains and I got it working. Problem is it is set up by default to rewrite_headers and accept the message. I need to reject it via the .qmail files.

I have one .qmail file and I need to copy it over every users .qmail file. Here is what I am trying.

Code:


cp ./.qmail /var/qmail/mailnames/*/.qmail

Where * can be any domain.

However the cp command complains that /var/qmail/mailnames/*/.qmail is not a directory. I already know that and want to overwrite the file. if I omit the .qmail file and do cp ./.qmail /var/qmail/mailnames/*/ it returns a message like 'Omitting directory example.com/ ....'

What am I doing wrong. All I want to do is copy one file to multiple folders which are all level one sub-folders of a parent folder we know the path to.

By the way ln -s gives exactly the same error

Thanks in advance

colucix 04-03-2008 07:14 AM

The problem is that when the wildcard is expanded the command line looks like
Code:

cp ./.qmail /var/qmail/mailnames/user1/.qmail /var/qmail/mailnames/user2/.qmail /var/qmail/mailnames/user2/.qmail
and the error message tells you that when you have multiple sources the last entry in the command line should be a destination directory. You cannot copy a single source file to multiple destination, whereas you can copy multiple sources to one destination. You can solve this by a simple for loop:
Code:

# for dest in /var/qmail/mailnames/*
> do
> cp ./.qmail $dest
> done

but first check that /var/qmail/mailnames/* expands to the directories you want to copy the file into. If it happens that you have a file in /var/qmail/mailnames, it will overwritten by .qmail. In this case you can refine the criteria as in
Code:

# for dest in $(find /var/qmail/mailnames -mindepth 1 -type d)
> do
> cp ./.qmail $dest
> done


decartes 04-03-2008 07:30 AM

Excellent, thanks for the quick response. Works a treat. See what you mean about refining. Nearly orverwrote my quota files.

Thanks again, this is the best tip I've had for ages. I badly need to develop my bash scripting skills, do you know of any resources?



Quote:

Originally Posted by colucix (Post 3109334)
The problem is that when the wildcard is expanded the command line looks like
Code:

cp ./.qmail /var/qmail/mailnames/user1/.qmail /var/qmail/mailnames/user2/.qmail /var/qmail/mailnames/user2/.qmail
and the error message tells you that when you have multiple sources the last entry in the command line should be a destination directory. You cannot copy a single source file to multiple destination, whereas you can copy multiple sources to one destination. You can solve this by a simple for loop:
Code:

# for dest in /var/qmail/mailnames/*
> do
> cp ./.qmail $dest
> done

but first check that /var/qmail/mailnames/* expands to the directories you want to copy the file into. If it happens that you have a file in /var/qmail/mailnames, it will overwritten by .qmail. In this case you can refine the criteria as in
Code:

# for dest in $(find /var/qmail/mailnames -mindepth 1 -type d)
> do
> cp ./.qmail $dest
> done



kaelthas 04-22-2008 06:17 PM

Is there a way to get this to work with directories that have spaces in their names? This is a great solution otherwise. Whenever I try this, it copies my file into directories with no spaces just fine, but if the directory has a space in the name, i end up with a file named the first part of the directory name in the directory above it.

Quote:

Originally Posted by colucix (Post 3109334)
The problem is that when the wildcard is expanded the command line looks like
Code:

cp ./.qmail /var/qmail/mailnames/user1/.qmail /var/qmail/mailnames/user2/.qmail /var/qmail/mailnames/user2/.qmail
and the error message tells you that when you have multiple sources the last entry in the command line should be a destination directory. You cannot copy a single source file to multiple destination, whereas you can copy multiple sources to one destination. You can solve this by a simple for loop:
Code:

# for dest in /var/qmail/mailnames/*
> do
> cp ./.qmail $dest
> done

but first check that /var/qmail/mailnames/* expands to the directories you want to copy the file into. If it happens that you have a file in /var/qmail/mailnames, it will overwritten by .qmail. In this case you can refine the criteria as in
Code:

# for dest in $(find /var/qmail/mailnames -mindepth 1 -type d)
> do
> cp ./.qmail $dest
> done



chrism01 04-22-2008 06:25 PM

iirc, you need to change the Internal Field Separator (IFS) shell var. By default it's space or tab or newline, and you want to reduce it to just a newline inside the shell script.
At the top put

IFS="
"
That's a newline (rtn) with surrounding quotes. This new definition will revert to normal when the shell prog exits.
Here's a good tutorial: http://rute.2038bug.com/index.html.gz , also http://www.tldp.org/LDP/abs/html/ & http://tldp.org/LDP/Bash-Beginners-G...tml/index.html

lcole 04-22-2008 06:41 PM

does this help?

1) link to Advanced Bash Scripting Guide:
http://tldp.org/LDP/abs/html/

2)
[lcole@harley ~]$ find ~/"space in name" -mindepth 1 -type d
/home/lcole/space in name/a
/home/lcole/space in name/b
/home/lcole/space in name/a b

kaelthas 04-22-2008 08:15 PM

thank you for the fast responses :) some new things to try, i'll keep you posted.

kaelthas 04-26-2008 04:09 PM

just to update, setting IFS worked great, thanks :)

Quote:

Originally Posted by kaelthas (Post 3129673)
thank you for the fast responses :) some new things to try, i'll keep you posted.



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