Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FTP using find command

Status
Not open for further replies.

dshaw21369

Programmer
Jul 8, 2002
64
0
0
Hi,
I'm new to unix and I'm was wondering if anyone might know how to do the following:

1.find files with *in extension in over 20 directories on Server A
2.FTP these files to 20 seperate directories on to Server B (where the script is running)

I'm interested in using the find command if I could.

thanks
 
To run the find command on server A you will need to enable rsh or ssh permission to execute remote commands from server B to server A (as you can't run external commands on the remote machine from an FTP session).

Then you could do something along the lines of:

[tt]rsh serverA "find /basedir -name \*in -print > /tmp/files
tar cvf /tmp/files.tar -I /tmp/files
rm -f /tmp/files"
ftp -nv serverA << HERE
user username password
bin
get /tmp/files.tar /tmp/files.tar
del /tmp/files.tar
HERE
cd /basedir
tar xvf /tmp/files.tar
rm -f /tmp/files.tar[/tt]

This first creates a file listing of the files you require and places it in /tmp/files. It then creates a tar archive using that file listing. Both of these are done through the rsh (remote shell) command to serverA.

It then starts and FTP session to transfer the file over, and then uses tar again to extract it. Annihilannic.
 
the problem I'm having is I have to get and put to specific directories.. so I was reading from a file that specifies these directories
 
Here is the code:
I would like to connect 1 time only because when running this code is opening and closing the connection for every file in the control file (the list of directories). any suggestions

export TERM=vt100
CurrDate=`date +%Y%m%d%H%M%S`
Logfile=/home/Log.$CurrDate
temp=&quot;/home//temp&quot;
controlfile=&quot;/home/ftp.ctl&quot;
newline=\\n
filepattern=&quot;*&quot;
ipaddress=&quot;xxx.xx.xx.xx&quot;
userid=&quot;xxxxxxxx&quot;
password=&quot;xxxxxxxx&quot;
trap '' 1 2 3 4 5 6 13 15 18 24 29
if ls &quot;$controlfile&quot; > /dev/null
then
for file in `cat $controlfile | cut -d: -f3 `
do
localdir=`cat $controlfile | grep &quot;$file&quot; | cut -d: -f2`
remotedir=`cat $controlfile | grep &quot;$file&quot; | cut -d: -f3`
tempdir=`cat $controlfile | grep &quot;$file&quot; | cut -d: -f1`

cd $localdir
if ping -c 3 $ipaddress > /dev/null
then
{
echo &quot;
open $ipaddress
user $userid $password
cd $remotedir/Pending
ascii
dir *.in listfile
mget *.in
quit
&quot;
} | ftp -i -n

else
clear
rm $temp/filename
rm $file/filelist
exit 8
fi
if ls &quot;$tempdir/listfile&quot; > /dev/null
then

for x in `cat $tempdir/listfile | cut -c 40-100`

do


cd $localdir
ls > filelist1

d=`date`
if ! cat filelist1 | grep &quot;$x&quot; > /dev/null
then
echo &quot;Please check&quot; $x &quot;is missing or cannot be located &quot;>> $Logfile
temp=`not_successful`
else

echo &quot;Got the file &quot; $x >> $Logfile

{
echo &quot;
open $ipaddress
user $userid $password
cd $remotedir/Pending
rename $x $remotedir/Archive/$x
quit


&quot;
} | ftp -i -n

fi
done

rm $tempdir/listfile
if ls | grep &quot;filelist1&quot; > /dev/null
then
rm $localdir/filelist1
fi
fi
y=`date +%Y%m%d%H%M%S`
echo &quot;timestamp&quot; $y$newline >> $Logfile
done
else
echo &quot;*********************Control file is missing****************&quot;
exit 8
fi


if temp=`not_successful`
then
echo &quot;Please check $Logfile for files not transferred&quot; |mailx -s &quot;FTP ERROR&quot; email
else
echo &quot;FTP was completed successfully &quot; >> $Logfile
echo &quot;FTP was completed successfully &quot;
fi

exit
 
Instead of running the ftp command every time you go around the 'for' loop you could just append all of the commands to a text file, and then run the ftp once after the loop to process all of the commands. Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top