Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • 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!
  • Students Click Here

*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.

Students Click Here


ftp multiple file transfer and rename

ftp multiple file transfer and rename

ftp multiple file transfer and rename

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?

RE: ftp multiple file transfer and rename

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.

RE: ftp multiple file transfer and rename

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.


RE: ftp multiple file transfer and rename


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?

RE: ftp multiple file transfer and rename

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.

RE: ftp multiple file transfer and rename

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.

RE: ftp multiple file transfer and rename

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

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!

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