Speaking in Tongues: FTP
12 July 2002
FTP—File Transfer Protocol—is one of the core Internet protocols. FTP’s purpose is simple, to enable file and directory transfer between hosts. Although e-mail and the Web have somewhat eroded the primacy of FTP in its arena, FTP is still the most effective and efficient way to transfer large amounts of data between hosts.
Apple includes an FTP server in Mac OS X and Mac OS X Server, though its configuration and handling are different between the two. For the purposes of this article, we won’t touch managing and FTP server, just managing the FTP process itself. There are several GUI FTP clients available, but we’ll focus on the tried-and-true command line tool. Start by launching the Terminal.
You’ll need an FTP server with which to work; use your own Mac OS X Server for now. Use Server Admin to ensure FTP Service is enabled, and that you have at least one Share Point setup. Connect to the Server using the ftp command, as in the example below:
ftp mrsgale.fates.org
(Replace mrsgale.fates.org with your Server’s name or IP address.) FTP will attempt to connect to the remote server, and when it has done so a message and prompt will appear:
Connected to mrsgale.fates.org. 220-------------------------------------------------------------------------------- 220-MINISTRY OF CATS OFFICIAL FTP SERVER 220- 220-AUTHORIZED USE ONLY. UNAUTHORIZED USERS WILL BE PERSECUTED AND PROSECUTED. 220- 220-Please log in below. 220-------------------------------------------------------------------------------- 220- 220 mrsgale.fates.org FTP server (Version: Mac OS X Server 10.3.2 - +GSSAPI) ready. Name (mrsgale.fates.org:atropos):
The lines beginning with “220” are FTP server responses; the text you see has been customized for my Server. Mac OS X Server uses a much simpler welcome message by default, consisting of the host name, “FTP server” and version. Notice how the last line prompts you for a Name, then includes information in parentheses. The first part is the server’s fully-qualified domain name; the second part is your username (your Short Name from Users & Groups in Server Admin), which is pulled from your connection. If the username is correct, you can hit Enter to proceed to the next step:
331 Password required for atropos. Password: Enter your password, and you’re logged in: 230-------------------------------------------------------------------------------- 230-Thank you for connecting to the Ministry of Cats. Enjoy your stay! 230-------------------------------------------------------------------------------- 230- 230 User atropos logged in. Remote system type is UNIX. Using binary mode to transfer files. ftp>
First, a brief diversion. FTP is intended for transferring files. In many cases, you don’t necessarily want to provide a local account for everyone who might want to transfer files from or to your FTP server. For that, you want a general role account that everyone can use, and FTP supplies the answer in the form of the “anonymous” or “ftp” account ("ftp” is deprecated, use “anonymous"). When you supply the username “anonymous,” FTP responds with a different message prior to the password prompt:
Name (mrsgale.fates.org:atropos): anonymous 331 Guest login ok, send your complete e-mail address as password. Password:
Anonymous FTP will accept any valid e-mail address as the password. Because anonymous FTP can be a security threat—particularly on machines where access to files is promiscuous—it is generally discouraged. Apple ships Mac OS X Server with FTP disabled by default, and requires an additional step to enable anonymous FTP.
The two lines above the ftp> prompt tell you important information about your FTP session. The first line, “Remote system type is UNIX.,” tells you what kind of operating system FTP thinks the server is running. Mac OS X Server is UNIX at its heart, so it is identified as such. There are other types of hosts, and each host type has slightly different underlying file structures and file system arrangements. The second line, “Using binary mode to transfer files.,” refers to the type of data stream FTP will use to transfer files. FTP supports several different kinds of modes; the important ones from our perspective are ascii, binary (image), and ebcdic. The last type, ebcdic, is unique to IBM mainframes and is decreasingly common on the modern Internet.
ascii transfer mode is for “plain text” documents where every character is within the first 128 glyphs—called ASCII, after the American Standard Code for Information Interchange, developed in the early days of computing. ASCII does not contain any accented characters, and as such is not a reasonable transfer mode for virtually any file on the modern Internet.
binary, or image, transfer mode uses the full eight bits in a byte to transfer files. Since all computer-based data is expressed in eight bit bytes, this means that every file will be transmitted precisely as it is stored. Binary mode is the most applicable method of transferring files on the Internet today, and Mac OS X Server defaults to that transfer mode. Unless you are specifically told otherwise, stick to binary mode.
Once you’re connected to an FTP server, you are placed in an environment similar to, but not the same as, your Terminal session. Many of the commands that are familiar from the command line—ls, cd, mkdir—are the same in the FTP session. There are some changes, however, and the following table outlines the commonly used commands in an FTP session.
|
ls remote-directory local-file |
|
Prints a file listing to the screen. By default, the remote directory in which the FTP session is located is printed. Use the remote-directory option to list a remote directory; remote-directory needs to be a full path, using the syntax of the remote system’s operating system (e.g., UNIX style for Mac OS X and other UNIX operating systems, “drive:\path” for DOS and Windows machines, “location:[path]” for VMS, etc.). Use the local-file option to save the listing to the specified local file. Listings are evaluated using the equivalent of the “ls -l” command. |
|
dir remote-directory local-file |
|
This command is exactly the same as ls, but uses a less UNIX-centric name. |
|
! command arguments |
|
This command starts a new shell process; with the optional command and arguments options, the shell will execute the command specified, then return to the FTP session. |
|
cd remote-directory |
|
Changes the working directory in the FTP session to the specified remote directory. The format for 'remote-directory' needs to match that of the remote operating system unless you are changing to a sub-directory, in which case it can just be the name of the sub-directory. |
|
cdup |
|
Changes the working directory in the FTP session to its parent, if remote permissions permit. |
|
lcd directory |
|
Changes the working directory of the FTP user. If no directory is specified, the working directory will be set to the user’s home. |
|
delete remote-file |
|
If permissions on the FTP server permit, the 'remote-file' will be deleted. |
|
mdelete remote-files |
|
If permissions on the FTP server permit, the list of 'remote-files' will be deleted. |
|
get remote-file local-file |
|
Retrieve the 'remote-file' from the FTP server. Specifying local-file will have the file renamed on the fly as it is transferred to the local computer. |
|
mget remote-files |
|
Retrieves the list of 'remote-files' from the FTP server. Simple substitution characters ('?' = one character, '*' = any number of characters) can be used to retrieve multiple similarly-named files. Note that mget is not designed to retrieve directories complete with contained files. |
|
put local-file remote-file |
|
If remote permissions permit, this command will store a copy of 'local-file' in the working directory on the FTP server. Specifying a value for remote-file will rename the file on the remote end. |
|
mput local-files |
|
If remote permissions permit, this command will store copies of the list of 'local-files' (subject to wildcard substitution as with mget) on the remote server. |
|
prompt |
|
This command toggles interactive prompting for multiple-action commands (mget, mput, mdelete, etc.). Interactive prompting is enabled by default, and will result in the server requesting confirmation of multiple-action commands for each entry in their option list. |
|
hash |
|
This command toggles whether hash marks ('#') will be printed for each kilobyte of data transfers. The default state is hashing is off. Since hash will print regardless of whether the transfer is to a local file or the screen, disable it before listing directories. |
|
quit |
|
Terminates your FTP session. |
There are additional commands; see man ftp for a complete description.
An example FTP session is included below, to demonstrate some FTP commands.
[mrspeel:~] atropos% ftp mrsgale.fates.org Connected to mrsgale.fates.org. 220-------------------------------------------------------------------------------- 220-Welcome to MrsGale.Fates.Org 220- 220-*** AUTHORIZED USE ONLY *** 220- 220-Activity on this server is logged. You must have a valid login and password 220-on this machine in order to perform FTP transactions. 220- 220-Contact atropos@fates.org if you have questions or concerns. 220-------------------------------------------------------------------------------- 220- 220 mrsgale.fates.org FTP server (Version: Mac OS X Server) ready. Name (mrsgale.fates.org:atropos): 331 Password required for atropos. Password: 230-------------------------------------------------------------------------------- 230-Thank you for using our FTP service. 230- 230-NOTE: excessive use of file bandwidth may result in erasure of files. Contact 230- atropos@fates.org if you need a large amount of storage. 230-------------------------------------------------------------------------------- 230- 230 User atropos logged in. Remote system type is UNIX. Using binary mode to transfer files. ftp> ls 200 PORT command successful. 150 Opening BINARY mode data connection for directory listing. total 856 drwxr-xrwx 3 root wheel 264 May 16 07:31 Apache.rasplug drwxr-xr-x 5 atropos staff 264 Dec 6 2001 CUPSurvey -rw------- 1 atropos staff 548 Jun 19 09:53 Deleted Items drwx------ 5 atropos staff 264 May 3 11:19 Desktop drwx------ 11 atropos staff 330 May 9 11:14 Documents -rw------- 1 atropos staff 505 Jun 3 14:09 Drafts drwx------ 32 atropos staff 1044 Feb 16 17:59 Library drwx------ 2 atropos staff 264 Dec 31 1969 Movies drwx------ 2 atropos staff 264 Dec 31 1969 Music drwx------ 2 atropos staff 264 Dec 31 1969 Pictures drwxr-xr-x 3 atropos staff 264 Nov 15 2000 Public -rw-r--r-- 1 atropos staff 406743 Jun 11 10:27 Sent Messages drwxr-xr-x 13 atropos staff 398 Jan 19 11:08 Sites -rw------- 1 atropos staff 183 Feb 28 16:23 baz -rw-r--r-- 1 atropos staff 21 Feb 28 15:20 foo~ drwx------ 22 atropos staff 704 Jul 12 13:08 mail -rwxr-xr-x 1 atropos staff 321 Feb 28 16:24 mailtesting drwxr-xr-x 8 atropos staff 264 Apr 23 14:17 playground -rw-r--r-- 1 atropos staff 35 Feb 28 16:54 recipients -rw-r--r-- 1 atropos staff 103 Feb 8 16:54 settermname 226 Transfer complete. ftp> hash Hash mark printing on (1024 bytes/hash mark). ftp> put test local: test remote: test 200 PORT command successful. 150 Opening BINARY mode data connection for directory listing. # 226 Transfer complete. 133 bytes sent in 0.000579 seconds (229706 bytes/s) ftp> dir 200 PORT command successful. 150 Opening ASCII mode data connection for directory listing. total 864 drwxr-xrwx 3 root wheel 264 May 16 07:31 Apache.rasplug drwxr-xr-x 5 atropos staff 264 Dec 6 2001 CUPSurvey -rw------- 1 atropos staff 548 Jun 19 09:53 Deleted Items drwx------ 5 atropos staff 264 May 3 11:19 Desktop drwx------ 11 atropos staff 330 May 9 11:14 Documents -rw------- 1 atropos staff 505 Jun 3 14:09 Drafts drwx------ 32 atropos staff 1044 Feb 16 17:59 Library drwx------ 2 atropos staff 264 Dec 31 1969 Movies drwx------ 2 atropos staff 264 Dec 31 1969 Music drwx------ 2 atropos staff 264 Dec 31 1969 Pictures drwxr-xr-x 3 atropos staff 264 Nov 15 2000 Public -rw-r--r-- 1 atropos staff 406743 Jun 11 10:27 Sent Messages drwxr-xr-x 13 atropos staff 398 Jan 19 11:08 Sites -rw------- 1 atropos staff 183 Feb 28 16:23 baz -rw-r--r-- 1 atropos staff 21 Feb 28 15:20 foo~ drwx------ 22 atropos staff 704 Jul 12 13:08 mail -rwxr-xr-x 1 atropos staff 321 Feb 28 16:24 mailtesting# drwxr-xr-x 8 atropos staff 264 Apr 23 14:17 playground -rw-r--r-- 1 atropos staff 35 Feb 28 16:54 recipients -rw-r--r-- 1 atropos staff 103 Feb 8 16:54 settermname -rw-r--r-- 1 atropos staff 133 Jul 12 14:06 test # 226 Transfer complete. ftp> hash Hash mark printing off. ftp> delete test 250 DELE command successful. ftp> quit 221-You have transferred 133 bytes in 1 files. 221-Total traffic for this session was 1908 bytes in 1 transfers. 221-Thank you for using the FTP service on mrsgale.fates.org. 221 Goodbye. [mrspeel:~] atropos%