<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <HTML> <HEAD> <META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.9"> <TITLE>FTP mini-HOWTO: Beginner's guide to using ftp</TITLE> <LINK HREF="FTP-4.html" REL=next> <LINK HREF="FTP-2.html" REL=previous> <LINK HREF="FTP.html#toc3" REL=contents> </HEAD> <BODY> <A HREF="FTP-4.html">Next</A> <A HREF="FTP-2.html">Previous</A> <A HREF="FTP.html#toc3">Contents</A> <HR> <H2><A NAME="s3">3. Beginner's guide to using ftp</A></H2> <P><B>A quick guide to using ftp.</B> <P> <P>The standard ftp program is the original ftp client. It comes standard with most Linux distributions. It first appeared in 4.2BSD, which was developed by the University of California, Berkeley. <P> <H2><A NAME="ss3.1">3.1 Running the <CODE>ftp</CODE> program</A> </H2> <P>It's easy to use ftp. Let's say you want to connect to the anonymous ftp site metalab.unc.edu, to download the latest Linux kernel source. <P> <P>At the command line, type: <P> <BLOCKQUOTE><CODE> <PRE> $ ftp metalab.unc.edu </PRE> </CODE></BLOCKQUOTE> <P> <P>The ftp program will attempt to connect to metalab.unc.edu. Another way to do this is to run <CODE>ftp</CODE> from the command line with no parameters, and use the <CODE>open</CODE> command, with the site name as an argument: <P> <BLOCKQUOTE><CODE> <PRE> $ ftp ftp> open metalab.unc.edu </PRE> </CODE></BLOCKQUOTE> <P> <H2><A NAME="ss3.2">3.2 Logging into an FTP server</A> </H2> <P>When you connect to an FTP site, it will ask you for a login (pressing enter will log in as your local user name, in this case, <CODE>foo</CODE>: We log in as <CODE>anonymous</CODE> or <CODE>ftp</CODE>, to get to the public archive. <P> <P> <BLOCKQUOTE><CODE> <PRE> 220 helios.oit.unc.edu FTP server (Version wu-2.6.0(2) Wed Nov 17 14:44:12 EST 1999) ready. Name (metalab.unc.edu:foo): </PRE> </CODE></BLOCKQUOTE> <P> <P> <P>Now, we enter a complete e-mail address as the password (this is what most public FTP sites request). <P> <BLOCKQUOTE><CODE> <PRE> 331 Guest login ok, send your complete e-mail address as password. Password: </PRE> </CODE></BLOCKQUOTE> <P> <P>After a successful login, the following information is given to us: <P> <BLOCKQUOTE><CODE> <PRE> Remote system type is UNIX. Using binary mode to transfer files. ftp> </PRE> </CODE></BLOCKQUOTE> <P> <H2><A NAME="ss3.3">3.3 File transfer types</A> </H2> <P>After you log in to an ftp site, <CODE>ftp</CODE> will print out the file transfer type. In our case, it is binary. Binary mode transfers the files, bit by bit, as they are on the FTP server. Ascii mode, however, will download the text directly. You can type <CODE>ascii</CODE> or <CODE>binary</CODE> to switch between the types. <P> <P>You want to download the kernel source, so you leave the file transfer type at binary. The binary type is also what you would use for any non-text files -- such as graphic images, zip/gzip archives, executable programs, etc. If in doubt, use binary mode. <P> <P> <H2><A NAME="ss3.4">3.4 Navigating and listing directories</A> </H2> <P>You do an <CODE>ls</CODE> to see a list of the files. The <CODE>ls</CODE> command on ftp servers is executed on the remote server, so the command line options that you can use with it vary from server to server. The most common options are generally available, check the manpage for <CODE>ls</CODE> for details. <P> <BLOCKQUOTE><CODE> <PRE> ftp> ls 200 PORT command successful. 150 Opening ASCII mode data connection for /bin/ls. total 33590 -r--r--r-- 1 root other 34348506 Dec 03 03:53 IAFA-LISTINGS lrwxrwxrwx 1 root other 7 Jul 15 1997 README -> WELCOME -rw-r--r-- 1 root other 890 Nov 15 13:11 WELCOME dr-xr-xr-x 2 root other 512 Jul 15 1997 bin dr-xr-xr-x 2 root other 512 Jul 15 1997 dev dr-xr-xr-x 2 root other 512 Jul 18 1997 etc drwxrwxrwx 11 ftp 20 4608 Nov 28 16:00 incoming lrwxrwxrwx 1 root other 13 Jun 04 1998 ls-lR -> IAFA-LISTINGS dr-xr-xr-x 17 root root 512 Jun 08 11:43 pub dr-xr-xr-x 3 root other 512 Jul 15 1997 unc dr-xr-xr-x 5 root other 512 Jul 15 1997 usr 226 Transfer complete. </PRE> </CODE></BLOCKQUOTE> <P> <P>If the <CODE>ls</CODE> command lists so many files that they scroll off the top of the screen, you can use Shift-PageUp to scroll up. This works in Linux console mode as well as in xterm or rxvt. <P> <P>On public FTP archives, the downloadable resources are usually held in the <CODE>/pub</CODE> directory. In this example, you already know that the kernel sources are in the directory <CODE>/pub/Linux/kernel</CODE>, so you type the following to get into that directory: <P> <BLOCKQUOTE><CODE> <PRE> ftp> cd pub/Linux/kernel 250-README for kernel 250- 250-What you'll find here: kernel sources and patches 250- 250- 250 CWD command successful. </PRE> </CODE></BLOCKQUOTE> <P> <P>The messages you see, which begin with "250", are information messages sent by the server. In this case, the ftp server is configured to automatically send you the README file when you cd into the directory. <P> <H2><A NAME="ss3.5">3.5 Downloading and uploading files</A> </H2> <P>Now, after doing another <CODE>ls</CODE>, you see that you want to <CODE>cd</CODE> into the v2.2 directory. You do yet another <CODE>ls</CODE>, and find the file you want to download. It is <CODE>linux-2.2.13.tar.gz</CODE>. So you type this: <P> <BLOCKQUOTE><CODE> <PRE> ftp> get linux-2.2.13.tar.gz local: linux-2.2.13.tar.gz remote: linux-2.2.13.tar.gz 200 PORT command successful. 150 Opening BINARY mode data connection for linux-2.2.13.tar.gz (15079540 bytes). </PRE> </CODE></BLOCKQUOTE> <P> <P>The ftp program has started saving the remote file <CODE>linux-2.2.13.tar.gz</CODE> as the local file <CODE>linux-2.2.13.tar.gz</CODE>. <P> <P>If you wanted to save it as the local file <CODE>foo.tar.gz</CODE>, you could have specified it like this: <P> <BLOCKQUOTE><CODE> <PRE> ftp> get linux-2.2.13.tar.gz foo.tar.gz local: foo.tar.gz remote: linux-2.2.13.tar.gz 200 PORT command successful. 150 Opening BINARY mode data connection for linux-2.2.13.tar.gz (15079540 bytes). </PRE> </CODE></BLOCKQUOTE> <P> <P>If you want to download more than one file at a time, you'll have to use the <CODE>mget</CODE> (multiple get) command. You can use <CODE>mget</CODE> together with a space-delimited list of filenames you want to download, or you can use wildcards with the <CODE>mget</CODE> command. For example: <P> <BLOCKQUOTE><CODE> <PRE> ftp> mget linux* </PRE> </CODE></BLOCKQUOTE> <P> <P>Would get all files starting with the string "linux". Normally, <CODE>mget</CODE> will prompt you for each file before it downloads it. You can toggle this by using the <CODE>prompt</CODE> command. <P> <P>Now let's say you've written a piece of software, and you want to upload it to MetaLab to be included in their Linux software archive. First, you'd change to the /incoming directory (most public FTP servers have a directory, usually called incoming or uploads, where files can be uploaded), then you'd use the put command: <P> <BLOCKQUOTE><CODE> <PRE> ftp> cd /incoming ftp> put foo.tar.gz local: foo.tar.gz remote: foo.tar.gz 200 PORT command successful. 150 Opening BINARY mode data connection for foo.tar.gz. 226 Transfer complete. 10257 bytes sent in 0.00316 secs (3.2e+03 Kbytes/sec) </PRE> </CODE></BLOCKQUOTE> <P> <P>The <CODE>put</CODE> command works the same way as the <CODE>get</CODE> command, so you can use <CODE>mput</CODE> to upload multiple files at the same time. You can also upload a local file with a different filename on the server by specifying the remote filename and/or pathname as an argument. <P> <P>What if the file <CODE>foo.tar.gz</CODE> is not in your current local directory when you try to upload it? You can switch local directories by using the <CODE>lcd</CODE> (local change directory) command: <P> <BLOCKQUOTE><CODE> <PRE> ftp> lcd /home/foo/ Local directory now /home/foo </PRE> </CODE></BLOCKQUOTE> <P> <H2><A NAME="ss3.6">3.6 Running shell commands</A> </H2> <P>The ftp client supports using the bang (!) to run local commands. For example, to get a listing of files in your current local directory, do this: <P> <BLOCKQUOTE><CODE> <PRE> ftp> !ls </PRE> </CODE></BLOCKQUOTE> <P> <P>The way this works is that ftp calls the shell (specified in the <CODE>$SHELL</CODE> environment variable), and it is the shell which runs <CODE>ls</CODE>. Thus, you can run any command-line which works with your shell simply by prepending "!" to it (the default shell in most Linux distributions is bash, the Bourne Again SHell). Please note that <CODE>!cd</CODE> does not work as you would expect, this is why the <CODE>lcd</CODE> command exists. <P> <H2><A NAME="ss3.7">3.7 Hash marks and tick</A> </H2> <P>Wouldn't it be nice if you could watch the progress while you're downloading a file with ftp? You can use the hash command to print out hash marks as you download a file: <P> <BLOCKQUOTE><CODE> <PRE> ftp> hash Hash mark printing on (1024 bytes/hash mark). </PRE> </CODE></BLOCKQUOTE> <P> <P>As you can tell, ftp will print a hash mark for every 1024 bytes of data you download. <P> <P>There is also a <CODE>tick</CODE> option. <P> <BLOCKQUOTE><CODE> <PRE> ftp> tick Tick counter printing on (10240 bytes/tick increment). This will print something to this effect as you download a file: Bytes transferred: 11680 </PRE> </CODE></BLOCKQUOTE> <P> <P> <H2><A NAME="ss3.8">3.8 Other ftp commands</A> </H2> <P>There are many other ftp commands. If you have the permissions to do so (which you should, if you are connected to your own private shell account), you can make a directory on the remote server using the <CODE>mkdir</CODE> command. You can remove a file on the remote server using the <CODE>delete</CODE> command, or <CODE>rmdir</CODE> to remove a directory. You can also change file permissions using the <CODE>chmod</CODE> command. <P> <P>For more elaborate information on using ftp, please see the online help in the ftp program (accessible by typing help with no arguments for a list of commands, or help <commandname> for specific help on a command). You can also read the Unix man page for ftp by typing <CODE>man ftp</CODE> at your command prompt. <P> <HR> <A HREF="FTP-4.html">Next</A> <A HREF="FTP-2.html">Previous</A> <A HREF="FTP.html#toc3">Contents</A> </BODY> </HTML>