LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to transfer files between two server after finding. (https://www.linuxquestions.org/questions/programming-9/how-to-transfer-files-between-two-server-after-finding-882578/)

Monika32011 05-25-2011 02:40 AM

How to transfer files between two server after finding.
 
Hello experts,

I want to search for 1 file (say test.txt) on first server and all the output of this search to be greped as per my requirement and then transfered on the second server at the same location where they were on first server.

for eg,
output of search of file (test.txt) >> output.txt
/tmp/test.txt
/home/cpan/test.txt
/opt/cpanel/test.txt

Now I want to grep this output to only related to cpanel,

for f in `cat output.txt`
echo $f | grep "cpanel"

if [ $? -eq 0 ]
then do scp.
But I am bit confused here, as in how to use scp command here.
scp $f root@second_server:/$f ?
Is this right? This doesnt seem to working.
Please advise.

Regards
Monika

dwhitney67 05-25-2011 06:04 AM

See if something like this will work for your needs:
Code:

#!/bin/bash

cat output.txt | grep cpanel | while read file
do
        dir=`dirname "$file"`
        scp "$file" root@second_server:"$dir"
done

Note: I placed $file and $dir in double-quotes in case either has a white-space in their respective file name.

TobiSGD 05-25-2011 06:26 AM

Quote:

Originally Posted by dwhitney67 (Post 4366440)
Code:

cat output.txt | grep cpanel | while read file

No need for cat here, this will do the job:
Code:

grep cpanel output.txt| while read file

Monika32011 05-26-2011 02:47 AM

thanks this worked :)

Code:

do
        dir=`dirname "$file"`
        scp "$file" root@second_server:"$dir"
done



All times are GMT -5. The time now is 01:47 PM.