Greg's Blog

helping me remember what I figure out

Command Line FTP

| Comments

This little article will introduce you to the wonderful and archaic way of accessing FTP sites using the command line. Please note that you can use these commands both under Unix and DOS.

Establishing a connection
When connecting to an FTP server from the command line, open a DOS prompt (select Start/Run and for Win9x type command or for NT type cmd) window. There you can now type:

ftp ftp.mysite.com

Should you need to connect to an FTP server on a diffrent port other than the default port you need to connect using the following set of commands:

ftp. You should now see the following prompt: ftp>. There type: open ftp.mysite.com 1234, where 1234 is the number of the port you are trying to connect to.

In either case you will be asked to login, so supply your username and password at the relevant prompts, right now let’s move on to the interesting bits.

Some useful commands
Before we get into downloading and uploading files here are some useful commands:

  • Type ?, to obtain a list of all availbale commands.
  • Type bin, to change to binary mode, this is usually used for anything other than text files upload and downloads.
  • Type ascii, to change back to ASCII mode.
  • Type hash, this while uploading and downloading gives you an incremental row of hashes marking the progress of either your upload or download.

Moving around remotely
Right now that you have logged on you need to know where you in are in the directory and tree and how to navigate, and this is where a little bit of Unix comes in handy. In order tpo find out where you are type pwd this stands for print working directory. To find out what is located in the directory you are currently in, type ls. This gives you a list of files and sub-directories, but not in a very legible format. So try typing ls -l. This gives you a better idea of what is what and if memory serves me well also a list of permissions on each item listed. Should that list get to long, i.e. rattle past you on the screen at the speed of light, type ls -l |more. You can now peruse the directory listing at your leisure as it will stop when it fills the screen. To bring up the rest, just press the space bar. You can change directories by typing cd. If you need to step through several directories and know the path, you can type this cd directpry1/subdirectory/etc. To backtrack out of a directory type cd .. and to backtrack several directories type cd ../..

Moving around localy
Right know you kow how to move around on the FTP server, but how do you get around your local machine? First of all to find out where you are, you can type lcd. This is the same as the pwd command and it gives your current path. Unfortunately I am not aware of a method for listing the contents of your local directory (so fire up explorer or whatever and take a peak). To change local directories you type lcd DirectoryName, to move back you type lcd ... And that’s all I know.

Uploading files
The command used to upload a file is put, so you would type something like this put MyFile.FileExtension. Pretty straightforward right? However there are a few more things you need to now. FTP distinguishes between Binary files and ASCII files, and if you happen to upload a file in ASCII format that should actually be in Binary the result might be less than successful. By default it is set to ASCII, so to change it to Binary, type Bin. You see a message informing you of the mode change. The next yo can do is use the hash command. THis displays a incremental row of hashes, symbolising the upload process. Why do you want to do this? Well you may well upload a huge file at one stage or another and by default the screen just sits there and gives you no indication of progress. For all you know it may well be hanging. So by using the hash command you can keep an eye on things and see how things are progressing.
Now you now how to upload single files, but how do you upload multiple files I hear you cry! To do this you use the mput command (multiple put?). The following example uploads all files with the extension “html”.
mput *.html
[Using this command put *.html will only grab the first html file in the directory]. Be careful when using mput *.* cause this tries to upload the entire contents of the directory, including subdirectories (only it creates files, rather than recreating the directory and it’s content and structure.

Downloading files
The concept is very similar than that of uploading files. You can again use the hash command to track progress. To download a file you use the getcommand, e.g. get MyFile.FileExtension. Similarly to download several files in one go type mget *.html, which in this case will download all the files with the extension html.

Deleting files and directories
To a delete a file, type the following command del FileName.FileExtension. To delete multiple files, type del *.FileExtension. To delete a directory, you can use the rm command, but you have to make sure that the directory is empty. You type rm DirectoryName.

Well that’s about it. Hope you found this introduction useful