Thursday, January 26, 2012

FTP and SFTP Beginners Guide with 10 Examples


FTP is File Transfer Protocol. SFTP is secure FTP. In this article let us review how to connect and login to a remote ftp server for downloading and uploading files using ftp or sftp command. Most of the ftp commands are applicable to sftp. So, wherever ftp is mentioned, you can use sftp also.

1. Connect to a FTP site

Connect to a particular FTP server using ftp command as shown below.
Syntax:
$ ftp IP/hostname

or 

$ ftp
ftp> open IP/hostname
You can directly open connection with a remote host using it’s IP or host name from the command line. You can also go to ftp prompt and use open command to connect with remote host.
It will ask you for the user name and password to login. On some public domain FTP server, you can use “anonymous” username with any email address as the password to connect.

2. Download a file using ftp

Use the get command to download file from a remote ftp server as shown below.
ftp> get FILENAME
You have to be in the right mode to download files. i.e binary or ascii mode. Use ascii mode for transferring text files, and binary mode for all other type of files.
Download the file and save it with another name. In the following example, index.html file will be downloaded and saved as my.html on the local server.
ftp> get index.html my.html
Fetching /home/groups/index.html to my.html
/home/groups/index.html                          100% 2886     1.4KB/s   00:02

3. Changing FTP Mode to binary or ascii

Go to ftp Ascii mode
ftp> ascii
200 Type set to A.
Go to ftp Binary mode
ftp> binary
200 Type set to I.

4. Uploading a file to FTP server

Use put command to upload a file to a remote ftp server as shown below.
ftp> put filename

5. Changing the remote and local directory

Apart from downloading or uploading a file, you may want to change either the remote or local directory, which you can do using cd and lcd respectively.
Change the remote server current directory using cd command
ftp> pwd
257 "/myftpserver" is current directory.
ftp> cd dir1
250 CWD command successful. "/myftpserver/dir1" is current directory.
ftp> pwd
257 "/myftpserver/dir1" is current directory.
Change the local machine current directory using lcd command
ftp> !
$ pwd
/home/sathiya/FTP
$ exit
exit
ftp> lcd /tmp
Local directory now /tmp
ftp> !
$ pwd
/tmp
Note:
  • executing ! takes you to the shell.
  • prompt starts with ftp> is ftp prompt.
  • prompt starts with is shell command line.

6. Listing the contents of remote directory from FTP

You can view the content of a remote directory using the ls / dir command.
ftp> ls

7. FTP Help

Type help or ? to view list of all available ftp commands.
For a detailed help on a particular ftp command use:
ftp> help COMMAND

8. Downloading multiple files with mget command

mget is for fetching multiple files from ftp server. You can use globs to download multiple files. For example, *.html will download all html files. The glob expansion are done on the remote server. So, it depends on the operating system of the remote server.
ftp> mget *.html
Fetching /ftptest/features.html to features.html
/ftptest/features.html                       100% 2256     2.2KB/s   00:01
Fetching /ftptest/index.html to index.html
/ftptest/index.html                          100% 2886     2.8KB/s   00:01
Fetching /ftptest/othertools.html to othertools.html
/ftptest/othertools.html                     100% 2282     2.2KB/s   00:01
Fetching /ftptest/samplereport.html to samplereport.html
/ftptest/samplereport.html                   100%   15KB   7.3KB/s   00:02
Fetching /ftptest/usage.html to usage.html
/ftptest/usage.html                          100% 2340     2.3KB/s   00:01
To view the file names before downloading, you can also use mls command as shown below.
ftp> mls *.html -
/ftptest/features.html
/ftptest/index.html
/ftptest/othertools.html
/ftptest/samplereport.html
/ftptest/usage.html

9. Uploading multiple files with mput command

Use mput to upload multiple files together. This works similar to the mget command. The following example uploads all the *.html file from local server to remote server.
ftp> mput *.html

10. Close a FTP connection

Without exiting the ftp prompt you may want to open a connection to another server. In that case, execute close command.
ftp> open ftp.your_server.com
Already connected to NNN.com, use close first.
ftp> close
221 Goodbye.
ftp> open ftp.your_server.com