≡ Menu

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
Add your comment

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

Comments on this entry are closed.

  • Tanmay June 23, 2010, 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

  • patricia February 23, 2011, 8:04 am

    can’t restart failed tranfer

  • ratle March 9, 2011, 6:21 pm

    very good article! thanks!

  • Room40 December 14, 2011, 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.

  • Husain January 18, 2012, 2:56 pm

    Good Aritcle. Thanks for posting.

  • Mohamed Larouci April 8, 2012, 3:33 pm

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

  • Shambhu TIwar May 11, 2012, 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

  • srikantan June 21, 2012, 4:45 am

    thanks for commands code usage details-very useful

  • Saker July 11, 2012, 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 ??

  • dhirendra September 20, 2012, 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.

  • Martin September 28, 2012, 3:29 pm

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

  • Lokesh October 4, 2012, 9:46 pm

    really helpful contents, thanks a lot for posting.

  • chitra March 15, 2013, 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 ?

  • Jon March 25, 2013, 11:12 am

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

  • Bill May 30, 2013, 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.

  • zenon June 3, 2013, 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?

  • Lokesh Jangir June 4, 2013, 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

  • Jeremy June 12, 2013, 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?

  • Lokesh Jangir June 14, 2013, 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.

  • Mangapathi September 11, 2013, 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.

  • Mangapathi September 11, 2013, 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.

  • Manoharan December 5, 2013, 1:17 am

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

  • sridhar April 14, 2015, 12:10 pm

    i am beginner on unix.This Article on FTP is very clear with step by step explanation.

  • dr. pfeiffer August 11, 2015, 11:48 am

    lcd..awesome command 😉

  • Niall Fagan September 3, 2015, 5:14 pm

    i am trying to download certain files via FTP from a particular folder but only that have been modified in the last 24 hours. I have successfully managed to use mget to get the file i want from the location i want but have not been able to select by when last modified.
    Would appreciate any help on this. I have been trying to use mtime but not sure if it works like this.

    Regards
    Niall

  • Rayala November 16, 2015, 12:39 am

    Hi Ramesh,

    I have a requirement like, I want to download files from ftp server and rename immediately.
    Single file I can rename but what about 100 of files how to rename in ftp.

  • Krishna Marella May 3, 2017, 9:01 am

    Hi,
    I have a question and Im using FTP to move files from windows to linux and wrote a batch script, the script works fine, but in that I have 4 different files and each one has its own put command, problem is if any one of the file is not there in the folder, rest of files are not getting moved, could you please help me in that move files even if one is not existing.