Linux to Mac
Automating FTP on the Mac
- 2009.03.04 - Tip Jar
Popularity: ![]()
![]()
![]()
![]()
![]()
There is no shortage of GUI FTP programs, but kicking it old school on the command line allows you to easily automate uploads and downloads. The best part is, there is nothing to install. Everything you need waits patiently behind the warm glow of a Terminal session.
The Mac Command Line FTP Program
The default command line FTP program in OS X 10.5 resides at /usr/bin/ftp
By all outward appearances and behavior, the Mac FTP program is the standard BSD version. The man page is the standard BSD main page and contains a wealth of useful information.
A typical command line FTP session is interactive and goes something like this:
- login to an FTP server
- issue commands (ls (list), get (download), put (upload))
- quit
If you have a repetitive FTP task, the fun quickly fades into a mind numbing exercise. This is where FTP automation shines.
The Magical .netrc File
What makes FTP automation possible is a magical, little known file called .netrc. The .netrc file is a plain text file that is hidden (the file name starts with a period) and lives in the root of your home directory. The .netrc file allows FTP to perform automatic logins to FTP servers based on the name.
The .netrc is not created by default. You have to create it manually. To create an empty .netrc file, open a Terminal and use the following commands:touch .netrc chmod 700 .netrc
It is critical that you issue the chmod command to set the permissions so that only the owner of the file can view it. If the permissions are not set correctly, the FTP client will assume it has been compromised and will refuse to use it.
Inside the .netrc, you define a block of settings for each FTP server you use, including the machine name, the login ID, and the password. Here is a typical block for a mythical FTP server:
machine myftpserver.com login myuser password mypasswordThere are additional settings that can be included. Check the FTP man page for more. You can test your settings by typing "ftp myftpserver.com" at a Terminal prompt, and it should automatically login. Note that you can store multiple FTP server logins in the .netrc file.
Sending FTP Commands from a BASH Shell Script
Once logins are automated, the final piece of the puzzle is to script a set of FTP commands. The following example uses an advanced BASH shell scripting technique called a "here" document to group the FTP commands to be sent to the server.
#!/bin/bash /usr/bin/ftp -d myftpserver.com << ftpEOF prompt put "*.html" quit ftpEOF
The FTP command is issued with the -d flag (debug mode) to make it more verbose. That makes any kind of error more obvious. The connection is made to myftpserver.com using the ID and password from the .netrc file. Once the connection is made, the rest of the commands are issued one at a time until the end of the "here" document at the second "ftpEOF". Note that any valid FTP commands can be sent. In the example, the prompt command tells FTP not to prompt for multiple file operations, then the put uploads all files with an .html extension. If you want to go the extra mile, you can extend the shell script and do things like reconnect to the FTP server to verify the file sizes of your uploads.
While there are several ways you can automate FTP, the nice thing
about this method is that it is portable to Linux or any other Unix
system.
Join us on Facebook. Follow us on Twitter.
Keith Winston is a recent Mac convert after five years of Linux on the desktop. He also writes for Linux.com and created CommandLineMac to focus on the Unix-y power of the Mac. If you find Keith's articles helpful, please consider making a donation to his tip jar.
Recent Linux to Mac Columns
- Apple's launchd Is Anti-Unix, 2010.07.13. Unix prefers programs that do one thing very well, but Apple's launchd takes the opposite approach.
- Automount OS X Home Directories Using OpenLDAP and Linux, 2009.09.28. It's possible to get an OS X Mac to automatically mount a Linux server using OpenLDAP. The tricky part is finding all the steps needed to make it work.
- Introduction to Autofs in Mac OS X, 2009.07.01. "Autofs is often used in enterprise environments to set up network-based home directories and other network mounts for users at login."
- More in the Linux to Mac index.
Links for the Day
- Mac of the Day: Mac IIfx, introduced 1990.03.19. This 'wicked fast' 40 MHz Mac trumped the 33 MHz DOS world.
- February 14 in LEM history: 98: A perfect compact Mac - 00: Extended computer warranties worth the cost? - Making your PC work with your Mac - 01: Customize Microsoft Word - 02: Quadra revives a passion for computing - 03: Real world performance - DIY Pismo screen replacement - Best Mac for writing - 03: Fastest browser on the Mac - 06: 15" MacBook Pro - Impressions of a newly acquired Lisa - Finding and using free WiFi - Apple should liberate OS 9 - 07: New Mac mini cheaper than upgrading a Power Mac - 08: Falling in love with OS X
- Support Low End Mac
Recent Content on Low End Mac
- Fix Home Button Delay, Tablet the Ultimate Mobile PC, iPad Notebook a Possibility, and More, iOS News Review, 2012.02.10. Also using your iPad at work, two photo editors, a new iPad text editor, Macally's magnetic iPad 2 stand, and more.
- White MacBook Goes End-of-Life, Logitech Touch Mouse Supports Gestures, Firmware Updates, and More, The 'Book Review, 2012.02.10. Also MacBook Air better than any Ultrabook, docks for MacBook Pro models, Intel offers improved SSDs, and more.
- Mac and iOS Browsers: Options Galore, Freeware Forum, 2012.02.10. Safari is adequate on Mac and great on iOS, but the range of good alternatives is stunning. LEM writers share their favorites.
- Apple's Support Lead Shipping, Smartphones Outsell PCs, OS X Ported to ARM by Intern, and More, Mac News Review, 2012.02.10. Also the power of Tex-Edit Plus, Google and Twitter are already censoring the Web, Snow Leopard Security Update, and more.
- LogMeIn: Remote Screen Sharing for the Rest of Us, Alan Zisman, Zis Mac, 2012.02.09. Configuring the Mac's built-in screen sharing to work over the Internet can be difficult or impossible. LogMeIn makes it easy.
- 15 Years Ago Motorola Unveiled the PowerPC G3, Low End Mac Round Table, 2012.02.06. The G3 processor was optimized for real world Mac software and made a big leap forward in efficiency.
- Don't Kill Caps Lock, Learning to Love the iOS Keyboard, and an Adaptive iPad Keyboard, Charles W. Moore, Miscellaneous Ramblings, 2012.02.06. The Caps Lock key has a useful function, the iPad's keyboard really is useful, and checking out an adaptive keyboard for the iPad.
- More links in our archive.
Recent Deals
- Best 17" MacBook Pro Deals
- Best iPod classic Deals
- Best eMac Deals
- Best MacBook Air Deals
- Best iBook G4 Deals
- Best iPad Deals
- Best Classic Mac OS Deals
- Best Apple TV Deals
- More deals in our archive.
About LEM Support Usage Privacy Contact
Follow
Low End Mac on Twitter
Join Low End Mac
on Facebook
Low End Mac Reader Specials
TypeStyler 11 is now in the Mac App Store!! -- Special Introductory Price of $59.95!! -- To Buy From The Mac App Store Click Here Now!! Or buy direct
from Strider Software.
Don't install Parallels to play poker online! Poker Mac will show you how to download and install a native Mac poker and Mac Casino applications in minutes.
Favorite Sites
MacSurfer
Cult of Mac
Shrine of Apple
MacInTouch
MyAppleMenu
InfoMac
The Mac Observer
Accelerate Your Mac
RetroMacCast
PB Central
MacWindows
The Vintage Mac Museum
DealMac
Deal Brothers
Mac2Sell
Mac Driver Museum
JAG's House
System 6 Heaven
System 7 Today
the pickle's Low-End Mac FAQ
Affiliates
Amazon.com
The Apple Store
The iTunes Store
PC Connection Express
GainSaver
Parallels Desktop for Mac
eBay

