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
Comments on this entry are closed.
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
can’t restart failed tranfer
very good article! thanks!
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.
Good Aritcle. Thanks for posting.
It’s a very interesting for linux users
Many thanks for your support
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
thanks for commands code usage details-very useful
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 ??
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.
hello , how i can connect to ftp and get file contents (with php function) on one file ?
really helpful contents, thanks a lot for posting.
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 ?
You should be able to use a wild card character with mget (i.e. mget test_0*.dmp)
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.
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?
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
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?
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.
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.
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.
Can anyone tell me about creating a directory in the remote server using sftp ?
i am beginner on unix.This Article on FTP is very clear with step by step explanation.
lcd..awesome command 😉
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
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.
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.