In this tutorial, we will learn how to use the FTP command in Linux. We will discuss what is FTP and the use of commands.
What is FTP?
FTP stands for File Transfer Protocol. This protocol lets the file transfer from one host to another host over the network or between client and server also. It is provided by the TCP/IP. FTP establishes two connections mainly:
- Data Connection: This connection is established at port 20 that opens and gets closed for each file transfer.
- Control Connection: This connection is established at port 21 and remains connected through the entire FTP session.
FTP is the Client-server based protocol. As FTP is used for the file transfer, it can transfer the following file types mainly:
- ASCII File: It is simply the text file. Note that, FTP uses a set of ASCII characters to communicate across control connection.
- EBCDIC File: It stands for Extended Binary Coded Decimal Interchange Code. It uses an 8-bit representation ( (a string of 8 0’s and 1’s) for alphabet or numeric characters.
- Image File: It can be in any format either JPG, PNG, or JPEG.
For more details, Check the FTP Official Documentation.
Common Options for the FTP command in Linux
- get: download the file from the FTP server.
- mget: download multiple files from the FTP server
- put: upload the file to FTP server
- mput: upload multiple files to the FTP server
- cd: change the path of the directory
- mkdir: make the directory in the current directory
- rename: change the name of the file
- delete: remove the file in the current directory
- help: list all the FTP commands
- rmdir: remove the current directory
- lcd: change the directory on the local machine
Let’s look at some commonly used commands in FTP.
Initialize an FTP Connection
The first step to use the FTP command in Linux is to establish a connection between the server and the client. You need to specify the remote server IP address or the domain name. We will use the FTP command to do so. You should know the credentials i.e. username and the password of the server.
Let’s have a look at the command below:
# ftp IP_address
You can see the connection has been set up. It would ask the credentials for authentication purpose.
How to Transfer Files using the FTP Command in Linux?
As discussed earlier, FTP is used to transfer the file between client and server. There are three modes of transferring the file:
- Retrieving the file: It simply denotes copying a file from the server to the client. In simple words, downloading the file from the server.
- Storing the file: To send the file from client to server. In simple words, uploading the file to the server.
- Retrieving the list: To get the list of files/directories from server to client.
Therefore, to do so we use two commands i.e. Get and put. Let’s understand each of these.
1. Retrieve the files (Server -> Client)
You want to fetch the data from the webserver. Therefore, you will be considered as a client. The GET command is used to fetch the data from the server to the client. It is considered as downloading the file. You can either download a single file or multiple files too.
Download the single file from the server
Once you are done with establishing the connection, you can transfer the files. The GET command is used to download the single file from the server.
To do so, type the following in the terminal:
ftp> get file_name
You can see that the file has been transferred to the client system.
Download multiple files using the FTP command in Linux
Earlier, we discussed how to download the single file. What if you want to download multiple files? The mget command is used to fetch multiple files from the server. Either you can mention the name of each file or mention the type of the file. Let’s have a look at the command below:
ftp> mget *.txt OR ftp> mget filename1 filename2 filename3
It will download all the text files listed at the server.
2. Uploading files to the server (Client->Server)
Earlier we discussed how to download the single or multiple files from the server. Let’s see how to store the data at the server through the FTP command
It is called uploading the files from the client system to the server. Similarly, here we can upload single and multiple files both. The put command is used to upload the file.
Uploading single file to the server
The client can upload single file using PUT command. It simply denotes copying the data from client to server. You just need to mention the file name. Note that, the files you want to upload, should be present at the local system.
Lets’s have a look at the command below:
ftp> put filename
Upload multiple files using FTP command in Linux
Earlier we discussed how to upload a single file, we can also upload multiple files too. The mput command is used to upload the multiple files to the server. All the files should be present on the local system. Either you can mention the file names or the file type.
Let’s have a look at the command below:
ftp> mput filename1 filename2 filename3 OR ftp> mput *.txt
3. Retrieve the List of the file/directories
As in the Linux system, we can display the list of the files/directories of any directory. Similarly, using the FTP command we can retrieve the list of the files in the current directory. The ls command is used to retrieve the list of files/directories. Let’s have a look at the command below:
Renaming the file using FTP
Earlier, we disucssed how to transfer the files. Let’s see how we can rename any file. It’s quite easy to do so. The rename command is used to change the name of the file. Let’s have at look command below:
ftp> rename filename changed_filename
You can also list the files and check whether it has been renamed or not. Use ls command to do so.
The file named test.txt has been changed to linuxtest.txt.
Delete the file using FTP command
One of the FTP commands is used to remove the files present at the server. The delete command is used to remove the files at the server. Let’s have a look at the command below:
ftp> delete filename
The file named linuxtest.txt has been successfully deleted. You can also check the same by listing the files using ls command. Let’s have a look below:
Creating the directory using FTP command in Linux
We can also create the directory using the mkdir command. You need to mention the directory name. Let’s have a look at the command below:
ftp> mkdir directoryname
You can also check the directory using ls command. The directory named “linuxfordevices” has been formed at the server. You can also change the path using the cd command followed by the directory name. Let’s have a look at the command below:
ftp> cd directoryname
The directory has been successfully changed.
Logout the server
You can logout of the server after successfully executing the FTP commands. Type “exit”, “quit” or “bye” to exit the FTP server. Let’s have a look at the command below:
ftp> bye OR ftp> exit OR ftp> quit
List all the FTP commands
Here we have described the common FTP commands. The help command is used to list all the options for the FTP FTP command in Linux. Let’s have a look at the command below:
That’s it. We have learned how to use FTP command in Linux i.e. how to upload, download, retrieve, and so on. If you still face any issue, do let us know in the comment section.