Unix 101: Command Line FTP Tutorial
Dateline: 12/18/00
What is FTP and how do I use it? Learn how to use the Unix command-line FTP client with this introductory-level tutorial.
What is FTP?
FTP, short for File Transfer Protocol, is a client/server protocol for sharing files between machines over a TCP/IP network.
The machine you are working on, the local machine, runs a program called an FTP client. There are many different kinds of graphical and command-line FTP clients. This tutorial will cover the basic command-line FTP client available on almost every Unix system.
The machine you are connecting to, often called the remote server or FTP site, runs a program called an FTP server.
The server requires users to login with a username and password before transferring files between their local machine and the remote server. Copying files from the remote server to the local machine is called downloading. Copying files to the remote server from the local machine is called uploading.
Anonymous FTP sites allow anyone to login with a username of anonymous or ftp and do not require a password.
The Internet is full of anonymous FTP sites that provide free software, documentation, images, music and more.
Using FTP
Connecting
Virtually every Unix system has a command-line FTP client, which is
invoked by typing ftp at the Unix prompt.
$ ftp
ftp>
The FTP prompt (ftp>) appears and waits for FTP commands.
The open FTP command can be used to connect to a remote server (also called an FTP site).
open machine
The machine specification can be a machine name on your local network, IP address or domain name.
In this tutorial, we'll connect to the anonymous FTP site ftp.freesoftware.com, which provides free Unix software.
ftp> open ftp.freesoftware.com
You can also connect to an FTP site by invoking the ftp command with the name of the remote machine. For example,
$ ftp ftp.freesoftware.com
is an equivalent means of opening a connection to ftp.freesoftware.com.
After successfully opening a connection, the remote server will ask you to login with a username and password. As with most anonymous FTP servers, ftp.freesoftware.com allows guests to login with the username anonymous.
Connected to ftp.freesoftware.com.
220 freesoftware.com FTP server ready.
Name (ftp.freesoftware.com:liz): anonymous
The password for a guest login is your full email address. This is typical for anonymous FTP sites.
331 Guest login ok,
send your email address as password.
Password: *************
After a successful login, the remote server will send an introductory message and then return to the FTP prompt to wait for another command.
230-Welcome to ftp.freesoftware.com -
230-There are 920 users out of 5000 possible.
230-
ftp>
Navigating Directories & Listing Files
Use the ls command to list files and directories on the remote server.
ls filenames
If no filenames are specified, ls will list all files in the current directory.
Filenames can be specified using wildcards including
| * |
Matches any number of characters.
Example: b*d matches bid, band, b123adfr56d but not bank. |
| ? |
Matches any one character.
Example: b?d matches bid, bud and bad but not band. |
For example, list files that begin with the letter l.
ftp> ls l*
150 Opening ASCII connection for 'file list'.
ls-lR
ls-lR.gz
226 Transfer complete.
List all files in the current directory on the remote server.
ftp> ls
150 Opening ASCII connection for 'file list'.
README
archive-info
pub
ls-lR
ls-lR.gz
etc
226 Transfer complete.
On most anonymous FTP servers, files available for download are stored in the pub directory. Use the cd command to make the pub directory the current directory.
ftp> cd pub
250 CWD command successful.
In this tutorial, we will be downloading a game from the pub/unixfreeware/game directory so execute the following command.
ftp> cd unixfreeware/game
250 CWD command successful.
File Transfer Types
There are two types of file transfers, binary and ASCII. A binary transfer will create an exact copy (bit by bit) of the file. Use binary mode to transfer executable and compressed files. The ASCII transfer type is used to copy text. Some systems use different formats to store text files. The ASCII transfer mode will translate between these formats so that the text file displays correctly after it has been copied to a new system.
Use the ascii or binary FTP command to change the file transfer mode used to upload or download files. File extensions can help you choose which transfer mode is appropriate for a file. See the Extensions and Transfer Modes section.
Downloading a File
The get command downloads a single file.
get remote-filename [local-filename]
Get will copy the file remote-filename from the remote server to the local machine. If local-filename is specified, the downloaded file will be named local-filename; otherwise it will have the same name as the remote file.[1]
For the next example, we start by listing the files in the current directory.
ftp> ls
200 PORT command successful.
150 Opening ASCII connection for 'file list'.
total 8333
00_index.txt
acm-4.7.tar.gz
gnugo-1.2.tar.gz
226 Transfer complete.
Now let's download the file 00_index.txt.
ftp> ascii
200 Type set to A.
ftp> get 00_index.txt pubindex.txt
The ascii command changes the transfer mode to ASCII, which is appropriate because this is a text file.
The get command downloads the file 00_index.txt from the remote server and saves it on the local machine with the name pubindex.txt.
Now let's download the file gnugo-1.2.tar.gz.
ftp> binary
200 Type set to I.[2]
ftp> get gnugo-1.2.tar.gz
The binary command changes the transfer mode to binary, which is appropriate because this is not a text file. The get command downloads the file gnugo-1.2.tar.gz. Because no local filename is specified, the local file will also be named gnugo-1.2.tar.gz.
Downloading Multiple Files
The mget command downloads multiple files.
mget remote-filenames
Remote filenames can be specified using wildcards (like those used with the ls command).
Downloaded files will have the same name as they did on the remote server.[1]
By default, mget works in interactive mode. In interactive mode, mget queries the user
before downloading each file. For example,
ftp> mget *.gz
mget acm-4.7.tar.gz? n
mget gnugo-1.2.tar.gz? y
200 PORT command successful.
150 Opening BINARY data mode connection
for 'gnugo-1.2.tar.gz' (27314 bytes).
226 Transfer complete.
ftp: 27314 bytes received in 9.6Sec 2.8K/sec.
downloads all files in the current directory with filenames ending in .gz.
However, we skipped downloading the acm-4.7.tar.gz file.
Use the prompt command to turn interactive mode off.
For example,
ftp> prompt
Interactive mode off.
ftp> mget *.gz
200 PORT command successful.
150 Opening BINARY mode data connection
for 'acm-4.7.tar.gz' (971391 bytes).
226 Transfer complete.
ftp: 971391 bytes received in 75Sec 13.0K/sec.
200 PORT command successful.
150 Opening BINARY mode data connection
for 'gnugo-1.2.tar.gz' (27314 bytes).
226 Transfer complete.
ftp: 27314 bytes received in 9.6Sec 2.8K/sec.
The prompt command turns interactive mode off so the mget command downloads all files matching *.gz without querying the user.
Note: the prompt command toggles interactive mode on and off, so executing it again will turn interactive mode back on.
ftp> prompt
Interactive mode on.
Uploading Files
Uploading is similar to downloading except put is used in place of get.
The put command uploads a single file.
put local-filename [remote-filename]
Put will copy the file local-filename from the local machine to the remote server. If remote-filename is specified, the uploaded file will be named remote-filename; otherwise it will have the same name as the local file.[1]
The mput command uploads multiple files.
mput local-filenames
Local filenames can be specified using wildcards.
Uploaded files will have the same name as they did on the local machine.
Executing Local Commands
You can execute a Unix command on your local machine from within the FTP client.
Precede the command with an exclamation point. For example,
ftp> !pwd
/usr/home/liz/docs
executes the pwd (print working directory)
command on your local machine.
Leaving FTP
When you are done transferring files, use the close or bye command. Bye will exit the FTP client and return to the Unix prompt. Close will close the connection with the remote server but not exit FTP, allowing you to open another connection.
ftp> close
ftp> Enter more FTP command.
ftp> bye
$ Back at the Unix prompt.
Next page
> FTP Command Summary
> Page 1,
2,
3
Notes:
[1] When you download or upload a file into a directory that already contains a file of the same name, one of three things will happen.
- The old file will be overwritten with the newly uploaded or downloaded file. You won't be given any warning or indication that this happened. This is very common!
- The uploaded/downloaded file will be renamed so it doesn't overwrite the old file. For example, if you are downloading a file named README, it may be saved as README-1 to avoid overwriting an existing file.
- The FTP program will query the user to overwrite the old file or skip the download.
Because most FTP clients will overwrite your files without warning, it is a good idea to download into a temporary directory and then move the files where you want them.
[2]
When you set the transfer mode to binary, many FTP clients respond with the message "Type set to I.". The I is short for image, which the server uses to indicate an exact binary copy.
|