FTP and SFTP Beginners Guide with 10 Examples

by SathiyaMoorthy on June 22, 2010

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

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 22 comments… read them below or add one }

1 Tanmay June 23, 2010 at 7:07 am

Nice article.
I am a beginner, and learn a lot from your articles.
Here the article said in beginning that “most of the ftp commands are applicable to sftp”, so I was also looking for some specific sftp command which are not available in ftp!!!

Thanks
-Tanmay

2 patricia February 23, 2011 at 8:04 am

can’t restart failed tranfer

3 ratle March 9, 2011 at 6:21 pm

very good article! thanks!

4 Room40 December 14, 2011 at 10:18 pm

Good, however one comment. Secure FTP is FTPS and use SSL/TLS for securing the connection. STFP is different and uses SSH for securing the conneciton.

5 Husain January 18, 2012 at 2:56 pm

Good Aritcle. Thanks for posting.

6 Mohamed Larouci April 8, 2012 at 3:33 pm

It’s a very interesting for linux users
Many thanks for your support

7 Shambhu TIwar May 11, 2012 at 6:46 am

Hello Sir,
As you written this article is very helpfull for beginner or experience persons for much more understanding the unix ftp or sftp

Thanks,
Shambhu

8 srikantan June 21, 2012 at 4:45 am

thanks for commands code usage details-very useful

9 Saker July 11, 2012 at 3:02 am

Hello Sir,
Thanks for you explanation and i want to ask something about SFTP, how can i change transfer mode between ASCII and BINARY in Shell Scripting ??

10 dhirendra September 20, 2012 at 8:09 am

can we have a ftp command which simply check the availability of a file with in a ftp folder?
for example we have a folder named as “myfolder” inside xyz.com and i wanna ensure that there is .txt file with in “myfolder”.
Please come up with solution if possible.
Thanks a lot in advance.

11 Martin September 28, 2012 at 3:29 pm

hello , how i can connect to ftp and get file contents (with php function) on one file ?

12 Lokesh October 4, 2012 at 9:46 pm

really helpful contents, thanks a lot for posting.

13 chitra March 15, 2013 at 7:14 pm

Is there way to mget or mput files numbers within a specific range?
e.g.

test_01.dmp
test_02.dmp
test_03.dmp
test_04.dmp
test_05.dmp

mget test_0[2-4].dmp ?

14 Jon March 25, 2013 at 11:12 am

You should be able to use a wild card character with mget (i.e. mget test_0*.dmp)

15 Bill May 30, 2013 at 1:08 pm

Please change the title. FTPS is secure ftp.

SFTP is a part of the SSH protocol suite, and doesn’t use the same commands as FTP or FTPS. See here.

16 zenon June 3, 2013 at 9:11 pm

Hi,

Assuming that you are “mput *.html”, how is the transfer order determined?

Files to be transfered.
A.html b.html 1.html

Which file get transfer first second and third?

17 Lokesh Jangir June 4, 2013 at 5:23 am

Files to be transferred –
a.html A.html B.html b.html 1.html 2.html

When you will do “mput *.html” , files will be transferred in this order-
1.html 2.html a.html A.html b.html B.html

it works like cp command in linux

18 Jeremy June 12, 2013 at 6:15 pm

Ok…. so how do you start from utter scratch? I have a website that I want to add content to and it uses sftp – I’ve never touched it in my life….

Do I use the web browser, where do I start from square one?

19 Lokesh Jangir June 14, 2013 at 5:43 am

No, not web browser. You can use it as FTP but use port 22. You can use any ftp client too as filezilla-client. It will open the directory and you can transfer the files to the server.

20 Mangapathi September 11, 2013 at 10:45 pm

Hi Ramesh,
I am java developer. I am trying to download files from SFTP server from different folders. I have set of jobs to do this. All jobs will run at the same time(ex: for every 30 mins) in parallel and tries to get files from respective folders. I am using JSch library for this. In that I am using get command(get(“*.*”,mylocalfolder);) to download files. During this files are getting merged.How is it possible? How the get command will work actually ? Please suggest me if you have any idea about this.

21 Mangapathi September 11, 2013 at 11:09 pm

merging means one file content is copied into another file. Ex: Folder A has a file with content as “Hai ! Welcome”. Folder B has file with content as “Hello, Thankyou” in the SFTP server. But after downloading to my local system file content in Folder B was “Hellow, Hai Welcome” and vice versa.

22 Manoharan December 5, 2013 at 1:17 am

Can anyone tell me about creating a directory in the remote server using sftp ?

Leave a Comment

Previous post:

Next post: