How to Use FTP from the Linux Command Line

The FTP (File Transfer Protocol) program lets you transfer files from one computer to another, over the internet or a LAN. It comes built in with Linux operating systems. It is based on the client-server architecture. In general, the command lets you interact with files on a remote server. With it, you can copy files, rename and delete them and much more!
Connecting to a Remote Server with FTP



The command line to connect to an FTP server is:

ftp www.xyz.com

For example, if you want to connect to “www.cornell.edu”, you would use the following command line:

ftp www.cornell.edu

Log In After You Are Connected

Once you are connected to the server, you need to login. If you are using a private server, you will need to have a username and password given to you by the administrator. Without that you won’t be able to connect to it. If you are trying to connect to a public server, most will let you log in with your username as “anonymous” and your email id as password. Some public servers will let you access them if you use “ftp” as both the username and the password. Enter your username and password when prompted. If you manage to successfully log in, the following prompt will (in most cases) be displayed:

ftp>

The server should also inform you that you are using a remote UNIX system and that the binary mode will be used to transfer files. The binary mode is used to download all non-text files, like images, executable files and zip files. If you want to download text files, you can switch to the ASCII mode. To do that, enter the following command:

ftp> ascii

To revert back to binary, use the following command:

ftp> binary

Using FTP Related Commands Once You Are Logged In

Now that you are logged into the server, you can begin to use the FTP commands. These commands will be different for different servers. To see a list of all the commands available on the current server, enter the following command line:

ftp> help

To view all the files and subdirectories that are currently present in the directory you have navigated to, use the following command:

ftp> ls

On public servers you will want to navigate to the pub directory. This is where the files you’re looking for are probably being kept. So how do you navigate to the pub directory? Use the cd command, like this:

ftp> cd pub

This will get you to the pub directory. Once here, you can use the ls command again to look at all the files present. Now suppose you wanted to download an image: waterfall.jpg. How do you do that? Use this command:

ftp> get  waterfall.jpg

The file will get downloaded to the local directory on your machine. If you have a file with the same name present on your machine, it will get overwritten. To prevent that from happening, you can rename the file you are downloading like this:

ftp> get waterfall.jpg newwaterfall.jpg



This will prevent any naming conflicts. What if you wanted to get multiple files? You can use the “mget” command for that (the names of the files should be separated with a blank ” “).

ftp> mget waterfall.jpg river.jpg lake.jpg


What if you want to upload a file to the remote server? Note that you’ll require write persmissions on the remote server. If you don’t already have them, you’ll have to request the system admin. Here’s how you can use the put command:

ftp> put yourfile.jpg

What if you wanted to upload multiple files? Use the “mput” command, which is just like the “mget” command.

ftp> mput yourfile1.jpg yourfile2.jpg yourfile3.jpg

Directory Settings

How do you find out which directory the files are getting downloaded to? Use the “lpwd” command, as follows:

ftp> lpwd

This will show you the directory where the files are being copied to. To change the directory, you type the path of a directory after the lcd command:

ftp> lcd path1>path2>path3