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

Scripting FTP Transfers

© June 2007 Tony Lawrence

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 "1801759@foo.bar.com 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.

Got something to add? Send me email.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> How can I automate an ftp transfer or ftp through a proxy?


Inexpensive and informative Apple related e-books:

Photos for Mac: A Take Control Crash Course

Take Control of IOS 11

El Capitan: A Take Control Crash Course

Take Control of iCloud

Take control of Apple TV, Second Edition

More Articles by © Tony Lawrence

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) [xyz.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


Printer Friendly Version

Have you tried Searching this site?

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.

Contact us

Printer Friendly Version

I think it’s a new feature. Don’t tell anyone it was an accident. (Larry Wall)

Linux posts

Troubleshooting posts

This post tagged:




Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode