CLI magic: File Transfer Protocol

69

Author: Joe Barr

Wake up, you goodness-to-GUI slacksters! It’s time to get up and get out of that hammock and put your feet on the ground and your hands on the CLI. This week we’re going to talk about data in motion. Taking a file from one place on the network. Putting it someplace else. Most often today some form of FTP, the File Transfer Protocol, is used to do those things. It’s been part of the Internet since there was an Internet.If you’re interested in FTP history, you might want to read RFC 959. It’s a great source of background information. In my opinion, there are few — if any — programs in the history of computing that have made a greater contribution to the global spread of free software than FTP.

Old school FTP

If you’re an old geek like me, you may very well still use the standard FTP offering included in most Linux distributions. It may not have all the bells and whistles, but it still gets the job done.

To get a file from a distant machine, you need to know either its name or IP address. You may also need to know a valid user ID/password combination. Even public sites often require a signon: user Anonymous and your email address for a password, for example.
Let’s give it a try. Enter the following at the command line:


ftp ftp.ncftp.com

where ftp.ncftp.com is the name of the site we want to connect to. That results in the following lines appearing in the terminal window:


Connected to ncftpd.com.
220 ncftpd.com NcFTPd Server (licensed copy) ready.
Name (ftp.ncftp.com:warthawg): anonymous
331 Guest login ok, send your complete e-mail address as password.
Password:
230-You are user #1 of 16 simultaneous users allowed.
230-
230 Logged in anonymously.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp>

Note that the FTP server we connected to allows “Guest logins,” and asks for your email address as a password. Note too, that it identified my system as being a type of Unix and defaulted to binary mode for transfers.

If you don’t use binary mode and you download binary data (graphic images, compiled code, compressed data, and the like), you’re going to end up with an unusable download that won’t be much good for anything.

Let’s look around and get the lay of the land on this server. We can use the same ls command with FTP that you know and love from your local command line. Here’s what the site had to say when I tried it:


ftp> ls
227 Entering Passive Mode (209,197,102,38,218,14)
150 Data connection accepted from 66.68.177.157:60380; transfer starting.
drwxr-xr-x 3 ftpuser ftpusers 512 Jul 17 1999 gnu
drwxr-xr-x 2 ftpuser ftpusers 512 Jul 20 17:36 libncftp
drwxr-xr-x 3 ftpuser ftpusers 512 Jul 12 01:05 ncftp
drwxr-xr-x 8 ftpuser ftpusers 512 Jul 11 23:41 ncftpd
drwxr-xr-x 3 ftpuser ftpusers 512 Jan 11 2001 unixstuff
drwxr-xr-x 5 ftpuser ftpusers 512 Nov 16 2002 winstuff
226 Listing completed.

I happened to already know that the file I wanted to grab lived in the ncftp subdirectory, so that’s where I went:


ftp> cd ncftp
250 "/ncftp" is new cwd.

Now we ask for another directory listing to see what’s available:


ftp> ls
227 Entering Passive Mode (209,197,102,38,218,15)
150 Data connection accepted from 66.68.177.157:60636; transfer starting.
drwxr-xr-x 2 ftpuser ftpusers 1024 Jul 12 01:03 binaries
-rw-r--r-- 1 ftpuser ftpusers 95832 Nov 21 1997 ncftp-1.9.5.tar.gz
-rw-r--r-- 1 ftpuser ftpusers 192937 Mar 19 1998 ncftp-2.4.3.tar.gz
-rw-r--r-- 1 ftpuser ftpusers 401561 Jul 10 11:04 ncftp-3.1.8-src.tar.bz2
-rw-r--r-- 1 ftpuser ftpusers 509028 Jul 10 11:04 ncftp-3.1.8-src.tar.gz
-rw-r--r-- 1 ftpuser ftpusers 647750 Jul 10 11:04 ncftp-3.1.8-src.zip
226 Listing completed.

Aha. There it is, the latest NcFTP client. I’ll grab the bunzipped version, since it compresses into a smaller tarball than gunzip. Here’s how:


ftp> get ncftp-3.1.8-src.tar.bz2

And the FTP server replies:


local: ncftp-3.1.8-src.tar.bz2 remote: ncftp-3.1.8-src.tar.bz2
227 Entering Passive Mode (209,197,102,38,218,18)
150 Data connection accepted from 66.68.177.157:61404; transfer starting for ncftp-3.1.8-src.tar.bz2 (401561 bytes).
100% |***********************| 392 KB 215.70 KB/s 00:00 ETA
226 Transfer completed.
401561 bytes received in 00:01 (215.68 KB/s)

All that’s left to do now is to say goodbye by entering the quit command:


ftp> quit
221 Goodbye.
warthawg@linux:~>

Reversing the process — that is, sending a file from your machine to the distant end — is a similar exercise. But insteading using the get filename, you say put filename.

If you’re wondering why I chose the site I did to demonstrate FTP, it’s because I have recently been chastised for using the old, standard-issue FTP client instead of a spiffy client like NcFTP. Now that I’ve downloaded it, I can check it out to see if it’s as much better as claimed.

If you’re new to FTP on the command line, I recommend you spend some quality time perusing the output from man ftp. Then when you’re down with that, giving NcFTP a whirl. That’s what I’m going to do.