FTP Commands
FTP is the way to move files between systems (FreeBSD 5.2.1)
Posted 04.03.2006 | Updated 23.05.2006 | Contributed by Andy Mallett


File Transfer Protocol is relatively straightforward in its implementation and it is only necessary to know a handful of basic commands to use it effectively. Broadly speaking, FTP is used to download files (often installers) from somewhere else or to upload files to another system. The other two main methods of file transfer are via web (http) and through mapped shares (see see Samba).

FTP is part of the TCP/IP group of protocols and is supported in all operating systems, including Unix and Microsoft. Using FTP under a DOS command prompt is almost identical to using it under Unix, as the whole system is based on Unix commands.
When FTPing using a Unix system however, there are a few more commands available. Here then, are the basics of Unix FTP..

FTP Who?

To FTP another system, you can use its IP Address, NetBIOS name or hostname. To use either of the last two, the names must be resolvable, but using the IP Address of the remote FTP Server should always work. Thus..

ftp flash (NetBIOS name) or ftp flash.it.net (hostname) or ftp 10.0.0.100 (IP Address)

To initiate an FTP session with another system, the other host must be running the FTP service and a valid username and password will be required to connect successfully. Once connected, the FTP account may grant read only or full access for uploading files and may jail some accounts to a specified directory.

Start, Help and Stop

To enter FTP mode simply type ftp. Note the prompt changes to ftp>. Once inside an FTP session, typing help will bring up a list of over 100 commands specific to FTP. Note that only about ten of these commands are required for normal usage.

# ftp
ftp> help
Commands may be abbreviated.  Commands are:

!       delete     idle     mode     pmlsd     reset     system
$       dir        image    modtime  preserve  restart   tenex
account disconnect lcd      more     progress  rhelp     throttle
append  edit       less     mput     prompt    rmdir     trace
ascii   epsv4      lpage    mreget   proxy     rstatus   type
bell    exit       lpwd     msend    put       runique   umask
binary  features   ls       newer    pwd       send      unset
bye     fget       macdef   nlist    quit      sendport  usage
case    form       mdelete  nmap     quote     set       user
cd      ftp        mdir     ntrans   rate      site      verbose
cdup    gate       mget     open     rcvbuf    size      xferbuf
chmod   get        mkdir    page     recv      sndbuf    ?
close   glob       mls      passive  reget     status
cr      hash       mlsd     pdir     remopts   struct
debug   help       mlst     pls      rename    sunique


Typing help followed by the commmand name brings up a somewhat minimalist explanation of the command in question..

ftp> help mkdir
                 mkdir           make directory on the remote machine


Typing bye will terminate the FTP session and exit the user back to the command prompt..

ftp> bye
#


Establishing the connection

The principle of file transfer under FTP is no different from any other form of network download. The file to be downloaded is sitting in a directory somewhere on the remote server and is to be downloaded to a specific directory on the local machine. The principle of FTP download follows three main steps..
  • Establishing the download directory on the local machine
  • Finding the file on the remote server
  • Downloading the file
Note the use of the terms throughout this page which describe this classic client-server relationship: local machine means the computer you are sitting at (FTP Client) and remote server means the FTP Server you are getting the file from.

FTP Client-Server relationship


By default, the download directory will be the directory you were logged onto before you typed the ftp command. Thus if I start the FTP session from my home directory, then that's where the file will eventually download to..

# pwd
/usr/home/andym
# ftp
ftp>


Sometimes I forget to change the default download directory before starting the FTP session. No problemo, this is fixable using the lcd command at any time during the FTP session. This command stands for local change directory and will alter the default download directory on the local machine..

ftp> lcd /usr/local/src
Local directory now /usr/local/src

Note the full path to the download directory must be used with lcd. This can also be confirmed with the lpwd command, local print working directory.

If you're already in an FTP session, then use the open command to connect to the FTP server. Login with an appropriate username and password..

su-2.05b# ftp
ftp> open andrewmallett.net
Connected to andrewmallett.net.
220---------- Welcome to Pure-FTPd [TLS] ----------
220-You are user number 1 of 50 allowed.
220-Local time is now 00:20. Server port: 21.
220-This is a private system - No anonymous login
220-IPv6 connections are also welcome on this server.
220 You will be disconnected after 15 minutes of inactivity.
Name (andrewmallett.net:andym): andym
331 User andym OK. Password required
Password:
230-User andym has group access to:  andym
230 OK. Current restricted directory is /
Remote system type is UNIX.
Using binary mode to transfer files.
ftp>


Finding and downloading the file

Start with the pwd to print the working directory, which means to show the current directory you're sitting inside on the FTP server. Next use the ls -la command to show a listing of all the files and sub-directories in the current directory on the FTP Server.

Use the cd (change directory) command to navigate the remote server's file system. The final destination on the remote server will be the remote directory which contains the desired file. It usually takes a few ls -la and cd combinations to reach the desired remote directory..

ftp> pwd
257 "/" is your current location
ftp> ls -la
229 Extended Passive mode OK (|||18886|)
150 Accepted data connection
drwxr-xr-x   26 32023    andym        4096 Feb 28 18:37 .
drwxr-x---   22 32023    99           4096 Feb  5 04:18 ..
drwxr-xr-x    2 32023    andym        4096 Dec 16 13:20 email
drwxr-x---    3 32023    12           4096 Jan 20 21:40 etc
drwxrwx---    6 32023    12           4096 Dec 18 07:05 mail
drwxr-xr-x    3 32023    andym        4096 May 30  2005 public_ftp
drwxr-x---   22 32023    99           4096 Feb  5 04:18 public_html
drwxr-xr-x    6 32023    andym        4096 Sep  6 10:20 tmp
lrwxrwxrwx    1 0        0              11 Jan  9 14:53 www -> public_html


Note that as usual, directories are indicated with a d on the far left hand side of the listing..

ftp> cd public_html
250 OK. Current directory is /public_html
ftp>ls -la
drwxr-xr-x   26 32023    andym        4096 Feb 28 18:37 .
drwxr-x---   22 32023    99           4096 Feb  5 04:18 ..
-rw-r--r--    1 32023    andym        1177 May 31  2005 tafeold.htm
-rw-r--r--    1 32023    andym       24142 May 31  2005 tafepage_moved.gif
drwxr-xr-x   26 32023    andym        4096 Feb 28 18:37 tech
-rw-r--r--    1 32023    andym        3444 May 31  2005 tech.gif
-rw-r--r--    1 32023    andym       26927 May 31  2005 tent_w2.jpg
drwxr-xr-x   22 32023    andym        4096 Oct 13 05:25 thewitchery
-rw-r--r--    1 32023    andym        1518 May 31  2005 transcreen.gif


Once the required file is in sight, it's time to download it to the local machine. This time I'm after uhoh.gif..

ftp> cd tech
250 OK. Current directory is /public_html/tech
ftp> ls -la
229 Extended Passive mode OK (|||64912|)
150 Accepted data connection
drwxr-xr-x   26 32023    andym        4096 Feb 28 18:37 .
drwxr-x---   22 32023    99           4096 Feb  5 04:18 ..
-rw-r--r--    1 32023    andym        1079 Jan 27 02:20 404.htm
-rw-r--r--    1 32023    andym        7631 Jan 27 02:21 Dgclock.class
-rw-r--r--    1 32023    andym        4150 Jan 27 02:22 Fade.class
-rw-r--r--    1 32023    andym        1032 Jan 27 02:26 uhoh.gif
-rw-r--r--    1 32023    andym        2975 Jan 27 02:26 undev.gif


The command to download a file is get, however there is one important point to note before downloading that file. FTP has two modes, ascii (for clear text files) and binary (for other files, like programs). The default mode for FTP is ASCII mode, but if a non-text file is downloaded in ACSII mode it can become corrupted. So before downloading a non-text file, change to Binary mode using the bin command..

ftp> bin
200 TYPE is now 8-bit binary
ftp> get uhoh.gif
local: uhoh.gif remote: uhoh.gif
229 Extended Passive mode OK (|||11857|)
150 Accepted data connection
100% |******************************|  1032      66.54 KB/s    00:00 ETA
226-File successfully transferred
226 0.025 seconds (measured here), 39.71 Kbytes per second
1032 bytes received in 00:00 (2.78 KB/s)


Note the

226-File successfully transferred
message, confirming the file has downloaded. I know of no way to perform a local listing while in the FTP session, other than opening another shell logged onto the download directory. Ending the FTP session will also of course allow a local file listing to occur..

ftp> bye
221-Goodbye. You uploaded 0 and downloaded 2 kbytes.
221 Logout.
su-2.05b# ls -la
total 26
drwxr-xr-x  2 andym  andym   512 Mar  4 16:49 .
drwxr-xr-x  6 root   wheel   512 Oct 29 12:11 ..
-rw-r--r--  1 andym  andym   767 Jun 10  2005 .cshrc
-rw-r--r--  1 andym  andym   248 Jun 10  2005 .login
-rw-r--r--  1 andym  andym   158 Jun 10  2005 .login_conf
-rw-------  1 andym  andym   373 Jun 10  2005 .mail_aliases
-rw-r--r--  1 andym  andym   331 Jun 10  2005 .mailrc
-rw-r--r--  1 andym  andym   815 Jun  9  2005 .profile
-rw-------  1 andym  andym   276 Jun 10  2005 .rhosts
-rw-r--r--  1 andym  andym   975 Jun 10  2005 .shrc
-rw-r--r--  1 root   andym   123 Jul  9  2005 file.txt
-rwxrwxr-x  1 root   andym   162 Jul  9  2005 m.sh
-rw-r--r--  1 root   andym  1032 Jan 27 18:26 uhoh.gif


Uploading files using FTP

Uploading is of course, the opposite of downloading and the upload command is put. Set the default upload directory and navigate the remote FTP Server's directories in the same way. As previously mentioned, you will require write access permissions to the relevant remote directory to be able to upload stuff.

Notes

  • Note that at all times Unix is case sensitive, so watch the spelling of those file and directory names
  • When using command line FTP through MS-DOS, some commands such as lpwd are not available