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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ftp multiple file transfer and rename

Status
Not open for further replies.

jboo

Programmer
Jan 4, 2001
12
GB
Unix to Unix ftp. Everyday I have a load of files (named UPLUPOxxxxx, where xxxxx is a unique identifier). I need to ftp these files to another system but when the transfer's finished (either individually or at the end - I'm not bothered) I need to rename the file(s) as UPOxxxxx, i.e. strip off the UPL prefix. Can I do this within a loop inside the ftp open/quit or do I have to open, login and quit for each file?
 
Why not write a script to change the file names first and then ftp with mput? Otherwise, you would have to write another script on the other server and call that when finished.
 
All these *nix guys talking about easily automated
procedures and they don't consider Expect :/

Expect was made to automate this kind of thing and
is a main stay language among sysads.
 
marsd,

You can just as easily ftp files via shell as you can expect. I am aware of expect and have used it on several occasions. Here is a shell example of ftp, which is easier to write.....


#!/usr/bin/ksh -p

/usr/bin/ftp -nv <<-EOF
open ${IP}
user someone someone
bin
cd ${DIR}
put ${FILE} or mput ${FILES}
close
EOF


And your done. I do not see where jboo says he is using expect as his main program to do this and even if he was, you can just write a shell wrapper to make the file changes, since he is using Unix, and then call the expect script. So what is the problem with not considering expect at this point?
 
He said he basically wanted a complete and (automated,
preferably I am guessing) transfer of files with the ability to rename the files.

While this is certainly possible, expects advantages are
numerous:

1.Helpful error detection is simpler/possible.

ex:A page could be sent to the user/admin on error
or on successful completion along with info on the
transfer.

2.Automation of file transfer itself is simpler along
with the possibility of complete logging of each session.

ex:Many clients demand that for mputs a CR is
entered after each listing sent. At least
&quot;-niv&quot; options have to be entered for your example
above.
Other situations calling for more control are bound to
appear with this kind of thing.
The renaming of each file can be done easily with
confirmation and error detection as each file is successfully copied

3.Non-anonymous ftp logins are easier and more
secure.

4.More than one session can be handled at a time.

ex: That would be very useful if in the future
if the user needed to copy multiple sets of files
to different hosts. Or if a command application(ssh)
is needed in conjunction with the ftp session.

5.recursive file glob pattern transfers are easier to manage. Transparent recursive directory copying can be achieved more easily.

If all that is wanted is a quick and dirty way of doing things, then a here document ftp control is sufficient.
I would never use this method after learning expect.
So my personal prejudice is showing I guess.
Thanks for the feedback.
 
you could open the ftp connection, output an ls to a file, sed/awk then file to produce a syntax of:
Code:
get <original file> <modified filename>
for each file that matches the ls criteria (or awk criteria) and then use that as the input to a second ftp session.

would mean putting the user and password in there somewhere ... although you could take in a username, password and if needed an identifier (for the ls) as variables and delete the sed/awk/script files after use.
 
Her is something quick and dirty without any error checking which should do what you want, didn't test it though:

DIR=<your dir>
# Check for files
cd $DIR
NUMFLS=`ls UPLUPO*|wc -l`
if [ $NUMFLS -gt 0 ];
then
cd $DIR
FILENAME=`ls UPLUPO*`
exec 1>$LOGFILE 2>&1
# Create upload script for multiple files
# First the standard bits...
echo &quot;open $REMHOSTIP&quot; > ftptmp
echo &quot;user <name> <pwd>&quot; >> ftptmp
echo &quot;ascii&quot; >> ftptmp
# And now the multiple puts
for i in $FILENAME
do
echo &quot;put $i&quot; >> ftptmp
done
# And finally the QUIT command
echo &quot;quit&quot; >> ftptmp
# Do the ftp bit.....
LIS=`cat fdsxftptmp`
echo &quot;$LIS&quot;|ftp -n
rm ftptmp
# Now the renaming
for i in $FILENAME
do
NEWNAME=`echo $i|cut -c 3-`
mv $1 $NEWNAME
done
# end

IBM Certified Confused - MQSeries
IBM Certified Flabbergasted - AIX 5 pSeries System Administration
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top