Smart questions
Smart answers
Smart people
Join Tek-Tips Forums

Member Login

Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

ftp multiple file transfer and rename

jboo (Programmer) (OP)
22 Oct 02 6:21
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?
pmcmicha (TechnicalUser)
22 Oct 02 9:53
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.
marsd (IS/IT--Management)
22 Oct 02 12:03
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.

pmcmicha (TechnicalUser)
22 Oct 02 14:03

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
cd ${DIR}
put ${FILE} or mput ${FILES}

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?
marsd (IS/IT--Management)
22 Oct 02 17:09
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

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

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
"-niv" options have to be entered for your example
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

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.
jad (Programmer)
23 Oct 02 4:49
you could open the ftp connection, output an ls to a file, sed/awk then file to produce a syntax of:
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.
aixmurderer (IS/IT--Management)
23 Oct 02 19:57
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 ];
        cd $DIR
        FILENAME=`ls UPLUPO*`
exec 1>$LOGFILE 2>&1
# Create upload script for multiple files
# First the standard bits...
echo "open $REMHOSTIP" > ftptmp
echo "user <name> <pwd>" >> ftptmp
echo "ascii" >> ftptmp
# And now the multiple puts
for i in $FILENAME
      echo "put $i" >> ftptmp
# And finally the QUIT command
echo "quit" >> ftptmp
# Do the ftp bit.....  
LIS=`cat fdsxftptmp`
echo "$LIS"|ftp -n
rm ftptmp
# Now the renaming
for i in $FILENAME
      NEWNAME=`echo $i|cut -c 3-`
      mv $1 $NEWNAME
# end

IBM Certified Confused - MQSeries
IBM Certified Flabbergasted - AIX 5 pSeries System Administration

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close