FTP
stands for File Transfer Protocol. It is a simple protocol to transfer files from one machine to another.
In Linux, we can establish an FTP connection between two machines by using the command ftp
. To successfully transfer files over FTP, we must have read permissions on the source system and write permissions on the target system.
When FTP was created, security was not a major concern. Any data, including files and passwords, transferred over FTP is not encrypted. Hence, it is strongly suggested not to use FTP over the internet. If we need to transfer the data securely, we need to use
scp
orsftp
.
To establish an FTP connection, we use the ftp
command. The syntax for the ftp
command is:
ftp [options] [host]
In the above snippet, options
can be replaced by several options that can alter the default behavior of the FTP connection. We can find the available options and their descriptions in the table below:
Option | Description |
-4 | Use only IPv4 to establish the FTP connection |
-6 | Use only IPv6 to establish the FTP connection |
-p | Enables the use of FTP if a firewall is blocking external connections to the local machine |
-i | Stops the terminal to prompt the user for confirmation for each file in case of multiple files |
-n | Stops the FTP to search for auto-login credentials in the /home directory and attempt auto-login upon initial connection |
-e | If a FTP executable was created, this option disables the history and command editing |
-g | Stops file name globbing |
-v | Verbose option enables the FTP to display details of each step on the terminal |
-d | Enables debugging |
host
is replaced by the IP address or domain name of the remote machine, e.g.:
ftp 192.168.1.1
ftp domain.com
The host address might also contain a port number.
If the connection is established successfully, the terminal will show a confirmation message. This confirmation message might be different for different servers.
Most FTP servers are usually password protected. Hence, the terminal will prompt the user to enter the username and password after the confirmation message.
If the username and password are correct, a confirmation message will appear, and the prompt of the terminal will change to ftp>
.
Once the connection is established, there are several commands that we can use over FTP. We will briefly discuss a few of those commands below:
There are multiple commands that we can use to perform different operations over FTP.
Following are some commonly used FTP commands with their descriptions:
Command | Description |
get | Download a single file from remote machine to local machine |
mget | Download multiple files from remote machine to local machine |
put | Upload a single file from local machine to remote machine |
mput | Upload multiple files from local machine to remote machine |
delete | Delete a file from the remote machine |
pwd | Print the present working directory on the remote machine |
ls | Print the files and folders in the current working directory on the remote machine |
mkdir | Create a new directory in the current working directory on the remote machine |
rmdir | Delete a directory on the remote machine |
cd | Change the current working directory on the remote machine |
lcd | Change the current working directory on the local machine |
help / ? | To enlist all available FTP commands |
We can find all the available FTP commands with their descriptions here.
Following are the examples of commonly performed operations over FTP and their commands:
We use the lcd
command to change the current directory on the local machine.
The syntax for the lcd
command is as follows:
ftp> lcd [new_path]
For example, the following snippet changes the current working directory to “NewFolder” on the local machine:
ftp> lcd NewFolder
We use the cd
command to change the current directory on the remote machine. We can use the ls
command to list the files and folders in the current working directory.
The syntax for the cd
command is as follows:
ftp> cd [new_path]
For example, the following snippet changes the current working directory to “NewFolder” on the remote machine:
ftp> cd NewFolder
We use the get
command to download a single file and the mget
command to download multiple files from the remote machine to the local machine.
The syntax for the get
and mget
commands are as follows:
ftp> get [filename]
ftp> mget [filename-1] [filename-2] [filename-3] ...
In the above snippets of code, filename
is replaced by the name and extension of the file.
Consider the following two examples:
ftp> get abc.txt
ftp> mget abc.txt def.txt
In the above two examples, the get
command is used to download a single file named ‘abc.txt,’ and the mget
command is used to download two files named ‘abc.txt’ and ‘def.txt’.
In the case of multiple files, the terminal prompts the user to confirm the download for each file.
We use the put
command to upload a single file and the mput
command to upload multiple files from the local machine to the remote machine.
The syntax for the put
and mput
commands are as follows:
ftp> put [filename]
ftp> mput [filename-1] [filename-2] [filename-3] ...
In the above snippets of code, filename
is replaced by the name and extension of the file.
Free Resources