How to use the Linux ftp command to up- and download files on the shell

In this tutorial, I will explain how to use the Linux ftp command on the shell. I will show you how to connect to an FTP server, up- and download files and create directories. While there are many nice desktops FTP clients available, the FTP command is still useful when you work remotely on a server over an SSH session and e.g. want to fetch a backup file from your FTP storage.

Step 1: Establishing an FTP connection

To connect to the FTP server, we have to type in the terminal window 'ftp' and then the domain name 'domain.com' or IP address of the FTP server.

Examples:

ftp domain.com
ftp 192.168.0.1
ftp [email protected]

Note: for this example we used an anonymous server.

Replace the IP and domain in the above examples with the IP address or domain of your FTP server.

The FTP login.

Step 2: Login with User and Password

Most FTP servers logins are password protected, so the server will ask us for a 'username' and a 'password'.

If you connect to a so-called anonymous FTP server, then try to use "anonymous" as username and an empty password:

Name: anonymous
Password:

The terminal will return a message like this:

230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp>

When you are logged in successfully. 


Successful FTP login.

Step 3: Working with Directories

The commands to list, move and create folders on an FTP server are almost the same as we would use the shell locally on our computer, ls stands for list, cd to change directories, mkdir to create directories...

Listing directories with security settings:

ftp> ls

The server will return:

200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
directory list
....
....
226 Directory send OK.



List directories

Changing Directories:

To change the directory we can type:

ftp> cd directory

The server will return:

250 Directory succesfully changed.


Change a directory in FTP.

Step 4: Downloading files with FTP

Before downloading a file, we should set the local FTP file download directory by using 'lcd' command:

lcd /home/user/yourdirectoryname

If you dont specify the download directory, the file will be downloaded to the current directory where you were at the time you started the FTP session.

Now, we can use the command 'get' command to download a file, the usage is:

get file


The file will be downloaded to the directory previously set with the 'lcd' command.

The server will return the next message:

local: file remote: file 
200 PORT command successful. Consider using PASV.
150 Opening BINARY mode data connection for file (xxx bytes).
226 File send OK.
XXX bytes received in x.xx secs (x.xxx MB/s).


Download a file with FTP.

To download several files we can use wildcards. In this example, I will download all files with the .xls file extension.

mget *.xls

Step 5: Uploading Files with FTP


We can upload files that are in the local directory where we made the FTP connection.

To upload a file, we can use 'put' command.

put file

When the file that you want to upload is not in the local directory, you can use the absolute path starting with "/" as well:

put /path/file

To upload several files we can use the mput command similar to the mget example from above:

mput *.xls

Step 6: Closing the FTP connection

Once we have done the FTP work, we should close the connection for security reasons. There are three commands that we can use to close the connection:

bye
exit
quit

 Any of them will disconnect our PC from the FTP server and will return:

221 Goodbye


Disconnect from FTP server

If you need some additional help, once you are connected to the FTP server, type 'help' and this will show you all the available FTP commands.

List all FTP commands

Share this page:

Suggested articles

17 Comment(s)

Add comment

Comments

By: dgrb

FTP is not secure, as it transmits usernames and passwords in plaint text. Anyone using a network sniffer can discover them.

No sensible site even enables FTP anymore.

Use SFTP or scp, but plesase DON'T use FTP...

By: till

Seems as if you haven't heard of FTPS yet. FTPS is FTP over SSL/TLS and all current FTP servers support FTPS, so FTP is a secure protocol when your server supports TLS and nobody can sniff any passwords as the connection is fully encrypted. just start the ftp connection with ftps://yourserver.tld instead of ftp://yourserver.tld and you are safe from password sniffing.

By: Sado

You might want to cover the ftp-ssl command as well, it's great for connecting to FTPS sites.

 

ftp-ssl -pivz secure "$FTP_HOST" 990

By: tina

#you can automate this in a script too , just an example below, ofc one need to parameterize HOST USER FILE, to #be functional in a script eg with getopts or something 

read -s -p "password:" passwd

function getfiles {

HOST='someftpserver'

USER='myftpuser'

FILE="somefile" 

ftp -p -v -n $HOST <<END_SCRIPT

quote USER $USER

quote PASS $passwd

type binary

cd /go/to/some/dir

get $FILE

bye

END_SCRIPT

#exit 0

}

getfiles

By: jshock

How do you login if your usrname is an email address? For example "ftp [email protected]@ftp.ftpserver.org" won't work. Once ftp sees the @ it assumes the rest is the server address. Quotes, escape character \ and unicode @ don't work either.

By: Younis Raza

Thank you for the FTP tutorial and I could easily follow it. Now would you be kind to let me know how to download sub directories like images, css, includes and so on...?

By: shey samson

It was great to stumble upon this post.

By: ano

Thank's ! <3

By: Peter Perhac

This was briliant. Succint, to the point, easy to skim, works. Thanks!

By: charles R

How can I to send a file via ftp using specific port automatically.

I tried of differet way but not work.

Ex,

ftp -n <ip> <<END_SCRIPT

quote USER  

quote PASS 

cd /path/dest 

bin

prompt

put /dir/to/orig/file.txt

bye

END_SCRIPT

By: daulet

Sorry what will be command to get files by specific date. for example i want to dowload files which were uploaded in Dec 1 for example

By: Anand

To connect to the FTP server when we have domain name and port as well.We can use following command:

ftp domainName port

eg: ftp abc.int.pn 2121

By: hhgiese

How can I supress the output of the LCD command (because it is not usefull in log-file)?

By: Abhinay

How do you use the follwing command: put /path/fileIs the file name is something dynamic like : $(date -d yesterday +"%Y%m%d")_Admin_DEV.csv

By: Tyrone

After I enter the put command then the file path. I keep receiving "not a plain file" or I get "No containing directory". What am I doing wrong? 

By: new

unable to open a .txt file from ftp...Please tell how to do that...The terminal says tat ia ma alerady connected to the ftp disconnect from there first....

 

By: unnikrishnan

Nice article..short and simple. Thank you