Files with Finesse
That’s basically how FTP works, although you need to know, of course, about 400 other odds and ends to use FTP effectively.
When is a file not a file?
When it’s a text file. The FTP definition specifies six different kinds of files, of which only two types are useful: ASCII and binary. An ASCII file is a text file. A binary file is anything else. FTP has two modes, ASCII and binary (also called image mode), to transfer the two kinds of files. When you transfer an ASCII file between different kinds of computers that store files differently, ASCII mode automatically adjusts the file during the transfer so that the file is a valid text file when it’s stored on the receiving end. A binary file is left alone and transferred verbatim.
Warning Patience is a virtue
The Internet is pretty fast, but not infinitely so. When you’re copying stuff between two computers on the same local network, information can move at about 200,000 characters per second. When the two machines are separated by a great deal of intervening Internet, the speed drops — often to a few thousand characters per second or fewer. If you’re copying a file that’s 500,000 characters long, it takes only a few seconds over a local network, but it can take several minutes over a long-haul connection.
You tell FTP which mode to use with the binary and ascii commands: ftp> binary
200 Type set to I.
ftp> ascii
200 Type set to A.
In the preceding example, the I is for binary or image mode (after 25 years, the Internet protocol czars still can’t make up their minds what to call it), and the A is for ASCII mode. Like most FTP commands, binary and ascii can be abbreviated by lazy typists to the first three letters — so bin and asc suffice.
How to foul up your files in FTP
The most common FTP error that inexperienced Internet users (and experienced users, for that matter) make is transferring a file in the wrong mode. If you transfer a text file in binary mode from a UNIX system to a DOS, Windows, or Macintosh system, the file looks something like this (on a DOS or Windows machine): This file
should have been
copied in
ASCII mode.
On a Mac, the entire file looks like it’s on one line. When you look at the file with a text editor on a UNIX system, you see strange ^M symbols at the end of each line. You don’t necessarily have to retransfer the file. Many networking packages come with programs that do ex post facto conversion from one format to the other.If, on the other hand, you copy something that isn’t a text file in ASCII mode, it gets scrambled. Compressed files don’t decompress; executable files don’t execute (or they crash or hang the machine); images look unimaginably bad. When a file is corrupted, the first thing you should suspect is the wrong mode in FTP.Tip If you are FTP-ing (Is that a verb? It is now) files between two computers of the same type, such as from one UNIX system to another, you can and should do all your transfers in binary mode. Whether you’re transferring a text file or a non-text file, it doesn’t require any conversion, so binary mode does the right thing.Tip Getting a directory listing before issuing a get or put command is often comforting, so that you can have an idea of how long the copy will take.
The directory thicket
Every machine you can contact for FTP stores its files in many different directories, which means that to find what you want, you have to learn the rudiments of directory navigation. Fortunately, you wander around directories in FTP in pretty much the same way as you do on your own system. The command you use to list the files in the current directory is dir , and to change to another directory you use the command cd , as in this example: ftp> dir
200 PORT command successful.
150 Opening ASCII mode data connection for /bin/ls.
total 23
drwxrwxr-x 19 root archive 512 Jun 24 12:09 doc
drwxrwxr-x 5 root archive 512 May 18 08:14 edu
drwxr-xr-x 31 root wheel 512 Jul 12 10:37 systems
drwxr-xr-x 3 root archive 512 Jun 25 1992 vendorware
... lots of other stuff ...
226 Transfer complete.
1341 bytes received in 0.77 seconds (1.7 Kbytes/s)
ftp> cd edu
250 CWD command successful.
ftp> dir
200 PORT command successful.
150 Opening ASCII mode data connection for /bin/ls.
total 3
-rw-rw-r-- 1 root archive 87019 Dec 13 2003 R
-rw-rw-r-- 1 root archive 41062 Dec 13 2003 RS
-rw-rw-r-- 1 root archive 554833 Dec 13 2003 Rings
drwxr-xr-x 2 root archive 512 May 18 09:31 administrative
drwxr-xr-x 3 root archive 512 May 11 06:44 ee
drwxrwxr-x 8 root 234 512 Jun 28 06:00 math
226 Transfer complete.
200 bytes received in 63 seconds (0.0031 Kbytes/s)
ftp> quit
221 Goodbye.
In a standard UNIX directory listing, the first letter on the line tells you whether something is a file or a directory.d means that it’s a directory — anything else is a file. In the directory edu in the preceding example, the first three entries are files and the last three are other directories. Generally, you FTP to a host, get a directory listing, change to another directory, get a listing there, and so on until you find the files you want. Then you use the get command to retrieve them.You often find that the directory on your machine in which you start the FTP program is not the one in which you want to store the files you retrieve. In that case, use the lcd command to change the directory on the local machine.
Technical Stuff What’s with all these three-digit numbers?
You may notice that whenever you give a command to FTP, the response from the remote host begins with a three-digit number. (Or you may not notice; in which case, never mind.) The three-digit number is there so that the FTP program, which doesn’t know any English, can figure out what’s going on. Each digit means something to the program.Here’s what the first digit means:1: The program has begun to process your request but hasn’t finished it.2: It has finished.3: It needs more input from you, such as when it needs a password after you enter your username.4: It didn’t work but may if you try again.5: You lose.The second digit is a message subtype.
The third digit distinguishes messages that would otherwise have the same number (something that in the computer world would be unspeakably awful).If a message goes on for multiple lines, all the lines except the last one have a dash rather than a space after the number.Note: Most FTP users have no idea what the numbers mean, by the way, so now that you’re one of the few who does know, you’re an expert.
To review:cd changes directories on the other host;lcd changes directories on your own machine. (You may expect cd to change directories correspondingly on both machines, but it doesn’t.)
What’s that name again?
Sometimes on your machine, you have to give a file a name that’s different from the name it has on a remote machine. (This statement is particularly true on DOS machines, on which many UNIX names are just plain illegal, as well as when you’re retrieving Macintosh or Windows files, which can contain spaces and special characters.) Also, if you need to get a bunch of files, typing all the get commands can be tedious. Fortunately, FTP has work-arounds for both those problems. Suppose that you find a file named rose and you want to download it as rose.gif because it contains a GIF-format image. First, make sure that you’re in binary mode, and then retrieve the file with the get command. This time, however, you give two names to get — the name of the file on the remote host and the local name — so that it renames the file as the file arrives. You do this as shown in this interaction:
ftp> bin
200 Type set to I.
ftp> get rose2 rose2.gif
200 PORT command successful.
150 Opening BINARY mode data connection for rose2 (47935 bytes).
226 Transfer complete.
local: rose2.gif remote: rose2
47935 bytes received in 39 seconds (1.2 Kbytes/s)
Next, suppose that you want to get a bunch of files that begin with ru . In that case, you use the mget (which stands for multiple GET) command to retrieve them. The names you type after mget can be either plain filenames or wildcard patterns that match a bunch of filenames. For each matching name, FTP asks whether you want to retrieve that file: ftp> mget ru*
mget ruby? n
mget ruby2? n
mget ruger_pistol? n
mget rugfur01? n
mget rush? y
200 PORT command successful.
150 Opening BINARY mode data connection for rush (18257 bytes).
226 Transfer complete.
local: rush remote: rush
18257 bytes received in 16 seconds (1.1 Kbytes/s)
mget rush01? y
200 PORT command successful.
150 Opening BINARY mode data connection for rush01 (205738 bytes).
local: rush01 remote: rush01
205738 bytes received in 200.7 seconds (1.2 Kbytes/s)
mget rush02?
Tip If you find that mget matches more files than you expected, you can stop it with the usual interrupt character for your system — typically Ctrl+C or Delete: ^C
Continue with mget? n
ftp> quit
221 Goodbye.
You can even interrupt in the middle of a transfer if a file takes longer to transfer than you want to wait.
You also can do an express mget , which doesn’t ask any questions and enables you to find exactly the files you want. To tell FTP not to ask you about each file, use the prompt command before you give the mget command, as this example shows: ftp> prompt
Interactive mode off.
ftp> mget 02-1*
200 PORT command successful.
150 Opening BINARY mode data connection for 02-10.gz (123728 bytes).
226 Transfer complete.
local: 02-10.gz remote: 02-10.gz 123728 bytes received in 2.8 seconds (43 Kbytes/s)
200 PORT command successful.
150 Opening BINARY mode data connection for 02-11.gz (113523 bytes).
226 Transfer complete.
local: 02-11.gz remote: 02-11.gz 113523 bytes received in 3.3 seconds (34 Kbytes/s)
200 PORT command successful.
150 Opening BINARY mode data connection for 02-12.gz (106290 bytes).
226 Transfer complete.
local: 02-12.gz remote: 02-12.gz 106290 bytes received in 2.2 seconds (47 Kbytes/s)
Here’s a file in your eye
Okay, now you know how to retrieve files from other computers. How about copying the other way? It’s just about the same procedure, except that you use put rather than get . The following example shows how to copy a local file called rnr to a remote file called rnr.new : ftp> put rnr rnr.new
200 PORT command successful.
150 Opening ASCII mode data connection for rnr.new.
226 Transfer complete.
local: rnr remote: rnr.new
168 bytes sent in 0.014 seconds (12 Kbytes/s)
(As with get , if you want to use the same name when you make the copy, leave out the second name.) The mput command works just like the mget command does, only in the other direction. If you have a bunch of files whose names begin with uu and you want to copy most of them, issue the mput command, as shown in this example (boldface indicates the user’s input):
ftp> mput uu*
mput uupick? y
200 PORT command successful.
150 Opening ASCII mode data connection for uupick.
226 Transfer complete.
local: uupick remote: uupick
156 bytes sent in 0.023 seconds (6.6 Kbytes/s)
mput uupoll? y
200 PORT command successful.
150 Opening ASCII mode data connection for uupoll.
226 Transfer complete.
local: uupoll remote: uupoll
200 bytes sent in 0.013 seconds (15 Kbytes/s)
mput uurn? n
(As with mget , you can use the prompt command to tell it to go ahead and not to ask any questions.) Tip Most systems have protections on their files and directories that limit where you can copy files. Generally, you can use FTP only to put a file anywhere that you could create a file if you were logged in directly. If you’re using anonymous FTP (see the section “No Names, Please,” later in this chapter), you usually can’t put any files to the other host.A bunch of other file-manipulation commands are sometimes useful, as in this example of the delete command:
delete somefile
(Replace somefile with the name of the file you want to delete.) This command deletes the file on the remote computer, assuming that the file permissions enable you to do so. The mdelete command deletes multiple files and works like mget and mput do. The mkdir command makes a new directory on the remote system (again assuming that you have permissions to do so):
mkdir newdir
(Replace newdir with the name of the directory you want to make.) After you create a directory, you still have to use cd to change to that directory before you use put or mput to store files in it.Tip If you plan to do much file deleting, directory creation, and the like, logging in to the other system by using ssh or telnet is usually much quicker (discussed in gory detail in Chapter 16) to do your work and using the usual local commands.