Lesson 1.4 FTP
Publishing with FTP
One of the requirements for this course is to maintain a Web site that contains each of your Java homework assignments. [See Assignment 1].
You'll have to transfer HTML pages, Java .class files, and images, from your local machine up to the appropriate directories on the server. FTP is the program you'll use to accomplish that.
For this lesson, you'll learn to use the command line version of FTP. (This is pretty much the same whether you're using Windows or UNIX.) Knowing how to use command line FTP is a useful skill to have, even if you eventually use a graphical, drag 'n' drop style FTP client, such as WS_FTP or CuteFtp.
Getting Connected with FTP
FTP stands for File Transfer Protocol; it is the way you move files between different computers on the Internet. Using FTP requires two pieces of software--an FTP server, running on a remote machine, and an FTP client, running on your local machine.
If you have Windows, then you already have a command line FTP client. To use the command line FTP client, you'll have to learn how to:
- Connect to an FTP server and login.
- Move around the FTP server to find the files you want or the directories [folders] where you want to place your files.
- Copy files to the server and retrieve files from the server.
Let's start by learning how to login to the FTP server.
Starting the Client
The regular FTP client doesn't have an entry on the program menu. Instead, you need to open up a "shell window", also known as an MS-DOS Prompt, as shown below.
Under Windows ME and Windows 2000/XP, the MS-DOS Prompt has been moved to the Accessories menu. Under Windows 2000/XP, it has also been renamed the "Command Window".
Connecting
Once the MS-DOS Window opens type ftp, followed by the name of the server you want to connect to. As an example, if you were taking this course with me at Orange Coast College, the server would be:
csweb.occ.cccd.edu
Press ENTER when your screen looks like the one below.
Logging In
If you're working at home, the FTP client will automatically dial your ISP. If it doesn't, you'll have to connect to the Internet manually, and then repeat the step above.
If you're connected to the Internet, your FTP client will make a connection--you can think of it as opening a channel -- with the web server.
Here is what you'll see when you connect successfully:
The line that starts with 220 is sent by the server to let you know that the connection was successful.
On the next line--the one that begins with User--enter your Web Server ID.
Password
The FTP server will make sure that you have an account on our machine. When it finds your name in the accounts database, it sends you a request for your password.
Press ENTER when you have typed your password.
Login Mistakes
If you type the wrong password, or, if you type the correct password but mistype your user name, the server will send you a "530 Login incorrect" response, and your login will fail.
When this happens, you don't need to disconnect and start all over again. Instead, type the user command at the ftp> prompt as shown below:
ftp> user user 1036-100
You will be reprompted for a new password, and you should be able to login successfully.
Logging Out
If you type your password correctly, the server will send you back a
230 User XXXXXX logged in.
After you've successfully logged in, you'll be presented with the ftp > prompt. All of your FTP commands will be typed at the ftp> prompt. This is very similar to (but not exactly the same as) the MS-DOS command prompt you met in Lesson 1.3.
One of the first commands you should learn is the command to log off. Unlike most of UNIX, the FTP log off command is bye, rather than exit. If you forget, however, as in the illustration below, you'll just be reminded and placed back at the ftp> prompt.
Working with FTP
When you first log on to the server, you are automatically placed in your own personal home directory.
The FTP server software will let you see files inside your own home directory, and inside subdirectories [folders] that you create. Your home directory is your Web directory.
To view the home page, enter the web server's address, as in the following example:
http://csweb.occ.cccd.edu/1036/100/
Since this address doesn't specify a specific HTML page, [like index.html], the Web server delivers the default Web page for that directory. On our system, the default Web page must be named index.html. If there is no default Web page, then the Web server show a listing of all of the files in the directory.
Seeing What's There
To see what files are available on the server machine, you can use the ls command. (This stands for list). As you can see in the picture below, my home directory contains only a single file named index.html. [This is because I have already published my home page. If you have not, then your directory listing will be empty.]
Under most Web servers, index.html is the name of the default home page, which you will create for Homework Assignment 1.
Sending and Retrieving Files
Once you've successfully logged in and learned to move about, the only thing left to do is to transfer files. After all, that's the point of the whole exercise, isn't it?
There are four commands you can use to send and retrieve files:
- put: sends an individual file from the client (your local machine) to the server.
- get: retrieves an individual file from the server and stores it on your local machine.
- mput: sends a batch of files from your local machine to the server.
- Use wildcards to specify the files to send: mput *.gif
- You will be asked to confirm before each file is sent.
- To turn of this "confirmation" feature, use the prompt command before you use mput.
- mget: like mput, this retrieves a batch of files from the server and stores them on your local machine.
- You will be asked to confirm each file transfer, but you won't be warned if an existing local file is overwritten.
Here is a session that logs onto my Web site and transfers the file index.html from my local disk to my Web site:
Using Directories
If you like, you can create new directories [folders] on your Web site by using the mkdir command. To create a new subdirectory inside your home directory called images, for instance, you would use the command:
ftp> mkdir images
after logging into your Web site like this:
The images directory is created inside your current working directory, [your home directory in this case], provided a directory or file with the name images doesn't already exist.
Changing Directories
Before you can upload files to your new directory, you first have to make the new directory your working directory. You do that using the "change directory" or cd command.
Here are some examples that use the cd command.
1. ftp> cd images
2. ftp> cd /images
3. ftp> cd /
4. ftp> cd ..
The first example changes to the images directory, providing that images is a subdirectory contained in your current directory. This is called a relative path.
The second example starts with a forward slash [the root directory], so it always takes you to the images directory contained inside your home directory. This is called an absolute path.
The FTP server treats your home directory as the root directory, so the third example changes to your home directory. Example four uses the special name .. (dot-dot) to mean "change to the parent of this directory." If you use dot-dot in FTP, make sure you leave a space after the cd and before the first dot.
Other Directory Commands
If you create a lot of directories, you can have a little difficulty remembering "where you are". When you get lost like this, it's easy to accidentally upload a file to the wrong folder, or to be unable to find the a file you want. When this happens, type in the pwd command, and the FTP server will print your current working directory.
Here is an example that creates the images directory, changes to the images directory, prints the working directory, and then changes back to the home directory:
Sometimes you want to change directories on your local machine as well as on the server. The cd command only changes the working directory on the server. To change directories on your local machine, you use the lcd command, which works just like the CD command under DOS.
You can delete a directory you've created by using the rmdir command. However, before you do so, you first need to delete all of the files contained in the directory, using the delete command.
Other Commands
There are two FTP commands that are very important for transferring images and Java .class files. Whenever you transfer a text file [this includes .html files], you want to transfer them in ASCII mode. The default transfer mode is usually ASCII.
However, when you transfer images or binary files such as Java .class files, you need to make sure you are NOT using ASCII mode. To do this, use the bin command like this:
ftp> bin
Always do this before sending or retrieving binary files. To set the transfer mode back to ASCII, you can use the ascii command:
ftp> ascii
To delete a file on the server you can use the dele command.
To get help on other commands, type ? from the command prompt.
FTP Command Reference
Here are some common FTP commands. You may find that your FTP client [or our FTP server] does not support all of these commands.
| ascii |
set transfer type to ascii for text files |
| bell |
beep when command finished |
| binary |
set transfer type to binary. This is the default type |
| bye |
terminate ftp session and exit |
| cd dir_name |
change remote working directory |
| cdup |
change remote working directory to parent directory |
| close |
terminate ftp session to remote machine |
| delete file_name |
delete remote file |
| dir |
display a list of the remote directory contents |
| get file_name |
receive a file |
| hash |
toggle printing `#' for each buffer transferred |
| lcd dir_name |
change local working directory |
| ls |
list contents of remote directory |
| mdelete file_names |
delete multiple files |
| mget file_names |
get multiple files |
| mkdir dir_name |
make directory on the remote machine |
| mput file_names |
send multiple files |
| open site_name |
connect to remote tftp |
| prompt |
force interactive prompting on multiple commands |
| put file_name |
send a file |
| pwd |
print working directory on remote machine |
| quit |
terminate ftp session and exit |
| rename old_name new_name |
rename file |
| rmdir dir_name |
remove directory on the remote machine |
| size file_name |
show size of remote file |
| status |
show current status |
| system |
show remote system type |
| tenex |
set tenex file transfer type |
| user |
send new user information (login information) |
| verbose |
toggle verbose mode |
Something to Talk About
Here's a little something you can try to make sure you've mastered FTP. Using command-line FTP, retrieve one of 1999 tax forms stored on the server:
ftp.irs.gov
For your user name, enter anonymous, and for your password, enter your email address. The forms are stored in the directory
/pub/irs-99/
The forms are PDF files [Adobe Portable Document Format] and they must be moved using binary [not ascii] mode.
Once you have downloaded the form, send it to your home directory (you need to have web server space for this course). Check to make sure that it arrived correctly, then delete it.
What commands did you need to use? Please continue to the next section of this lesson.
|