APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed
RSS Feeds RSS Feeds

(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version
-> How can I automate an ftp transfer or ftp through a proxy?

Scripting FTP Transfers

How can I automate an ftp transfer?

There are actually numerous ways to do that. If your script is complex, I'd use use Perl Net::FTP or Kermit.

You can also use "here" files fpr simple cases:
 ftp -n $HOST <<-EOF
 cd /wherever
 prompt off
 mget '*'

Finally, ftp already has a somewhat primitive scripting language built in to it. "ncftp" or "lftp have even more capability, but here's a basic .netrc (see man netrc) for normal ftp.

 ----$HOME/.netrc 600 perms --
 machine somewhere.com login mylogin password mypass macdef
 lcd /appl/fp/merge
 cd /appx/data/50/XFR/Data
 put artrx.tab TRXFER.dat
 machine someothermachine.org login whatever password pass macdef
 prompt off
 machine yetanother ...

The first example (somewhere.com) logs in, changes to a local directory /appl/fp/merge, then changes to /appx/data/50/XFR/Data on the server and "puts" a file.

With this in place, the command "ftp somewhere.com" will do the "put". You could set "prompt off" and use "mput" or "mget" in the .netrc also.

The second just logs you in to "someothermachine.org" , turns on hash, etc. and then you can type your own commands.

You can fully script more complex things with:

 echo "machine somewhere.com login mylogin password mypass
 macdef init" > $HOME/.netrc
 echo "lcd /appl/fp/merge" >> $HOME/.netrc
 echo "cd /appx/data/50/XFR/Data" >> $HOME/.netrc
 for i in *.tab
 echo "put $i ${i%tab}.dat" >> $HOME/.netrc
 echo "quit" >> $HOME/.netrc
 echo " " >> $HOME/.netrc
 # always end a macdef with a blank line
 chmod 600 $HOME/.netrc
 ftp somewhere.com

FTP through a proxy (netrc)

Fri Mar 12 20:24:04 GMT 2004 FTP through a proxy (netrc)

In case you ever run into a situation where you need to ftp through a proxy server and want to use a .netrc file, this worked for me:

machine ftp.xyz.com login tyua166 passwd tyua166dt 
macdef init
user "[email protected] 15181" M2UDGHP
..(commands here)

The "tyua166" and "tyua166dt" is the login to the proxy, not the ftp site. In this case, the proxy required us to go to a specific port (15181) and the password was M2UDGHP.

The quotes around the user name and port number are required because of the port number.

If this page was useful to you, please help others find it:  


More Articles by - Find me on Google+

Click here to add your comments
- no registration needed!

Wed Jun 20 08:33:31 2007: 3034   anonymous

Many thanks for your examples !!! Great help for the actual task I'm working on. The only problem I get is the blank line at the end of the .netrc file.

Here is my file :

echo "machine my.remote.server login mylogin password mypass
macdef init" > $HOME/.netrc
echo "lcd /home/postgres/scripts/activite/export" >> $HOME/.netrc
echo "cd sauve" >> $HOME/.netrc
for i in *.sql
echo "put $i " >> $HOME/.netrc
echo "quit" >> $HOME/.netrc
echo " " >> $HOME/.netrc
# always end a macdef with a blank line
chmod 600 $HOME/.netrc
ftp my.remote.server

and here is the result I'm getting :

Connected to my.remote.server.
220 ProFTPD 1.2.10 Server (ProFTPD Default Installation) [XXX.XX.XX.XX]
500 AUTH not understood
500 AUTH not understood
KERBEROS_V4 rejected as an authentication type
Macro definition missing null line terminator.
Remote system type is UNIX.
Using binary mode to transfer files.

Any suggestion to solve this problem ?


Chris from France

Wed Jun 20 08:44:14 2007: 3035   anonymous

Sorry !!! I should'nt have put a space character on the blank line. Now, it works great. Thanks


Fri Sep 21 06:41:57 2007: 3143   JohnZou

i am so sorry to trouble than i make my shell like this:

echo "machine my.remote.server login mylogin password mypass
macdef init" > $HOME/.netrc
echo "lcd /home/tempt" >> $HOME/.netrc
echo "cd test" >> $HOME/.netrc
echo "prompt" >> $HOME/.netrc
echo "mget *t" >> $HOME/.netrc
echo "quit" >> $HOME/.netrc
echo " " >> $HOME/.netrc
chmod 600 $home/.netrc

but i can not login my server.hope you can give me help soon.Thanks.here is the information like this waiting for input user and password.

Connected to
220(vsFTPd 2.0.1)
502Command not implemented
502Command not implemented
KWEBEROS_V4 rejected as an authentication type
Name (

Fri Jul 25 13:07:19 2008: 4444   ljuban

Note: Just add additional machine macro i your .netrc script at the end. Like folowing:
machine dummy
login dummy
password dummy
macdef init
mput *

Tue Jul 29 16:45:31 2008: 4451   EvanCarroll

Dynamically creating .netrc files is categorically stupid. Macdef is *only* for sites which you never intend to frequent in the ftp shell -- you should make mention of this in your tutorial. You might want to do something more akin to:

SHELL=/bin/sh ftp -i <<EOF

or something like that.

Tue Jul 29 16:53:22 2008: 4452   TonyLawrence

I'll disagree strongly. I think Perl Net::FTP is a far smarter way to script transfers, but scripting .netrc files is certainly NOT stupid - but using the <<EOF absolutely is..

Tue Jul 29 22:23:37 2008: 4453   EvanCarroll

The heredoc that was originally given is a *good idea.* My arguement is against the writing to ~/.netrc to abuse macdef. Scripts that impliment this method will:
Be insecure if your home is mounted on a unencrypted-transit file system (exportfs).
Be insecure if anyone has write access to your home. (ex. creates a fifo which you mistakingly write to -- you don't check for it)
Fatally err if you can't write to home (ex read only fs, or the like)
The list can go on, but why should I?

The shell here-doc method works in most any shell, and doesn't require appending to a configuration file -- which by definition is not supposed to be dynamic anyway.

In other news, Net::FTP isn't a program, it is library, which isn't half bad or good, but requires an understanding of perl. Ckermit has a restrictive license and is hardly warranted for just FTP use. ncftp on the other hand is a totally cross-platform scriptable application for ftp, which while a farily stagnant works well. Non x-platform solutions you might be interested in would be curlfs or any other fuse based abstraction of ftp. In summary, dynamically appending to netrc is an all around bad soluation.

Consider simply echoing the script to ftp via stdin, rather than the *stupid* netrc voodoo the commenter is trying.

Tue Jul 29 22:27:08 2008: 4454   TonyLawrence

I flatly disagree.

"Here" files often cause hung sessions - .netrc files do not. If you are worried about security, deal with it.

Tue Jul 29 22:33:19 2008: 4455   TonyLawrence

By the way, do you know your website resolves to ???

Tue Jul 29 22:52:59 2008: 4456   EvanCarroll

lol, good one. If you file a bug report I'll be more than happy to take a look at it for you and see about resolving it. There is no reason one of these should hang while the other one doesn't. You might consider checking that you have the '-i' flag (so you never wait for input), and that your shell isn't obscenely old. Use, ltrace, and strace, to narrow down where it is hanging.

My web site resolves to because of a court order to have it removed.

Tue Jul 29 22:58:11 2008: 4457   TonyLawrence

I wonder what that says about you...

But regardless: knowing that it doesn't exist, why advertise it??

And you think using .netrc is stupid :-)

Tue Jul 29 23:17:58 2008: 4458   EvanCarroll

.netrc isn't stupid, is is just being abused via macdef. I use macdef for the reason it it is there, to bootstrap every /bin/ftp session with a macro. I have some some servers where the ftp homedir is permed -rwx, and you have to blindly chdir someplace that has perms that work for you. In this case .netrc is very handy.

re: evancarroll.com: it will exist again, soon enough, when I win the case.

Tue Jul 29 23:21:18 2008: 4459   TonyLawrence

Well, best of luck with your case and we'll just continue to disagree about this.

Tue Apr 6 14:45:45 2010: 8374   Defaro


Hi all,
Iīve tried this script and it didnīt work for me, because I really donīt know where should I copy the .netrc file. I just copied it to a any directory and the script didnīt find any file (of course).

Please, Where should I copy this file (.netrc)?

Thank you very much.

Tue Apr 6 15:55:19 2010: 8375   TonyLawrence


In your home directory. "man netrc"

Wed Oct 27 10:00:37 2010: 9074   VirajDodanwala


Thank you very much for your Help. This was very helpful for me. This helps me to transfer some files from Srilank to Maldives.


Thu Feb 3 11:27:47 2011: 9273   Bagbot


Many thanks.. This is a very useful script.. Many thanks again

Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments

If you want a picture to show with your comment, go get a Gravatar

Kerio Samepage

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments

Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

I am a Kerio reseller. Articles here related to Kerio products reflect my honest opinion, but I do have an obvious interest in selling those products also.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.


This post tagged:

       - FAQ
       - FTP
       - Popular

My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!

book graphic unix and linux troubleshooting guide

Buy Kerio from a dealer
who knows tech:
I sell and support

Kerio Connect Mail server, Control, Workspace and Operator licenses and subscription renewals

Click and enter your name and phone number to call me about Kerio® products right now (Flash required)