Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A few techniques for using command line FTP UNIX
While we all wait for Finder-level FTP access to be repaired, don't miss out on the cool hidden features of command line FTP program. Check "man ftp" for full details, but read the rest of the article for three neat examples I discovered just this morning. I believe many, and even perhaps all, of these hints will be usable by 10.1 users as well, but I am not certain; FTP appears to have been upgraded at least somewhat from 10.1 to 10.2, as evidenced by the new textual progress bars during file transfers.

Here are three things you can do with the command-line FTP client:
  1. You can automatically grab a file using a one-line command. For instance, if a user named gwbush had a password yeehah and wanted to download iraq-invasion-plans.txt from, he would type:
    The file would then be downloaded without a need for any further interaction. If he wanted to download a file outside his home directory, he could, but he'd need to type the first slash as its hex code — %2F — instead of the actual character. Check the manual page for further information.

  2. You can set up a file entitled .netrc in your home directory to automatically log you into FTP servers you commonly use. Using the information from the above example, if gwbush wanted to be able to automatically log onto without having to type in his username and password each time, he would create a file called .netrc in his home directory containing this:
    machine login gwbush password yeehah
    And bam!, auto-login!

  3. In that same .netrc file, you can set up macros to perform batches of FTP commands you do frequently. You can just make an alias for long directory names:
    macdef my-mail
    cd /usr/local/maildirs/gwbush
    Or you can automate downloading a file and deleting the remote copy (something that can't be done via the technique in hint number one):
    macdef spamgrab
    lcd /Users/gwbush/Desktop
    cd /usr/local/maildirs/gwbush
    get spam.txt
    dele spam.txt
    These macros need to be prefaced with a dollar sign when typing them in — in other words, $my-mail or $spamgrab.

    The neat thing is that you can then echo this to the FTP command and, by doing so, automate nearly any batch process. For example, if you had auto-login already set up, you could run the above spamgrab batch from the Terminal by typing:
    echo $spamgrab | ftp
  4. And don't forget that for convenience's sake, you can create an alias for any of the above commands. You could create an alias spamgrab for echo $spamgrab | ftp, and then just type spamgrab in Terminal any time you wanted to grab that file off the remote server.

    Or you could do it via an AppleScript application (through AppleScript's do shell script command) so you could get the file without loading Terminal. It all depends on what environment you spend the most time in, the GUI or the CLI. If you do it that way, though the above example needs an extra slash, so the AppleScript would be:
    do shell script "echo \$spamgrab | ftp"
I hope you find this as convenient as I have so far! :)
  • Currently 0.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (0 votes cast)

A few techniques for using command line FTP | 12 comments | Create New Account
Click here to return to the 'A few techniques for using command line FTP' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Correction to Above Hint
Authored by: Anonymous on Sep 04, '02 11:14:30AM

On #4, the AppleScript command should have TWO backward slashes prior to that dollar sign.

[ Reply to This | # ]
can't put pasword in .netrc
Authored by: dm2243 on Sep 04, '02 11:26:10AM

I get an error message telling me as much.

[ Reply to This | # ]
RE: can't put pasword in .netrc
Authored by: nvdingo on Sep 04, '02 12:13:59PM

you can put your password in there, as long as you `chmod go-r .netrc`
if you don't the actual error that appears is:

220 FTP server ready.
ftp: Error: .netrc file is readable by others.
ftp: Remove password or make file unreadable by others.

which tells you how to fix the problem

[ Reply to This | # ]
...a few more ftp hints
Authored by: Auricchio on Sep 04, '02 12:58:25PM
1. macdef init is run whenever you start ftp, so you can put useful stuff in that macro. For example, if the remote system doesn't identify itself as a unix system, ftp won't switch to binary mode. It's easy to put that command in the init macro. (Note that you can use binary mode for transferring ascii files just fine; I always use binary mode for everything.)

2. If you run ftp as HOME=. ftp xxxx, ftp will use the .netrc file in the current directory instead of your home directory. For the curious, this command causes the shell to set $HOME for the duration of the current command. Csh users probably need to do this in a subshell, e.g. (set HOME=. ; ftp xxxx) .

[ Reply to This | # ]
Use SFTP not FTP
Authored by: Chas on Sep 04, '02 01:05:39PM

I strenuously urge people to never use FTP, since passwords are transmitted in plaintext. Set up SSH and use SFTP instead.

[ Reply to This | # ]
ftpd comments
Authored by: victory on Sep 04, '02 07:24:09PM

Yeah, I'd noticed that the ftp server with 10.1 and earlier seemed to be a generic one that was probably a part of OSX's BSD legacy. With 10.2, it appears as if Apple is using lukemftpd, which is an open source ( ) offering. I wonder why? (Personally I wish they had chosen proftpd) Perhaps it's simply because lukemftpd is a project in active development that already has stuff like IPv6 support.

As to the overall security issue, I agree with Chas that sftp/SSH is far more secure. However, like telnet, FTP can still be useful in limited instances, such as on a small internal LAN. Also, ftp is ubiquitous -- nearly every version Windows comes with a cmd-line FTP client. Often it's quicker for me to flip on ftp-access on my PowerBook and grab a few files from a nearby Wintel box than it is to set up SMB/Windows filesharing (even with the improvements in 10.2)

[ Reply to This | # ]
Speaking of which....
Authored by: victory on Sep 04, '02 07:37:55PM's another trick that's a DEFINITE SECURITY RISK, but may be helpful as a last resort if you're on a machine that doesn't come with a native FTP client (MacOS 8.x and earlier, Windows 3.1, etc) Many people forget that most web-browsers work perfectly fine for doing FTP downloads. The only tricky part is including the username and password in the URL. Basically, you use the form:


A useful trick in a pinch, but risky for all else. Forget packet-sniffing, all someone has do is look at your screen (or history cache) to see your password!

[ Reply to This | # ]
It is improved but...
Authored by: jasenko on Sep 04, '02 10:33:56PM

.. why would you bother with it?
ncftp is much better ftp client, and it compiles without problems on jaguar
make install
and off you go
It gives you so much more.

On the positive not ftp was definetely improved, I like tab completion and if it has safe bookmark tool like ncftp maybe it could be worth using.
BTW, curl is also great if downloading files is all you need (for known URLs)

[ Reply to This | # ]
problem with macro within shell script
Authored by: ste5e on Dec 17, '02 09:27:25AM

The macro works fine when I use it manually, but I can\'t get it to work from within a shell script. For example, if I define a macro called \"grabit\" and call it from a shell script that has alread logged in to an ftp server like so:

echo $ grabit (or $ grabit)

. . .the ftp macro is never run. However, I can run it fine manually.

[ Reply to This | # ]
chmod of .netrc
Authored by: patrickoehlinger on Dec 20, '02 06:09:31PM
ftp complains that:

ftp: Error: .netrc file is readable by others.
ftp: Remove password or make file unreadable by others.

Solve the problem with
i>chmod 0700 ~/.netrc.

[ Reply to This | # ]
Error in #3
Authored by: patrickoehlinger on Dec 20, '02 06:46:40PM
At the end of #3 you should type
echo /$spamgrab | ftp
instead of
echo $spamgrab | ftp
since this would echo what ever is stored in the variable $spamgrab.

By the way, I used the following macdef to quickly update my online ical:
macdef syncCal
cd public_html/ical/calendars
send ~/Library/Calendars/public.ics public.ics

To get the update done, I type:
echo $syncCal | ftp myftp

[ Reply to This | # ]
Mac OS X HINTS stole our backward slashes
Authored by: patrickoehlinger on Dec 20, '02 06:49:18PM
Well seems like Mac OS X HINTS doesn't like backward slashes.

[ Reply to This | # ]