ftp Command in Linux



The ftp command in Linux transfers and receives files from the remote server. FTP is a File Transfer Protocol that is widely used to transfer or receive files from a remote system over the Internet.

Table of Contents

Here is a comprehensive guide to the options available with the ftp command −

Note − FTP is not a secure protocol. Its traffic is unencrypted and vulnerable to sniffing. It is recommended to use FTP only on trusted networks.

Prerequisites to Use ftp Command

Various FTP servers can be used to transfer files over the Internet, one of them is vsftpd. vsftpd stands for Very Secure FTP Daemon. It is a lightweight and secure FTP server that allows both anonymous and non-anonymous FTP access.

To install vsftpd on Ubuntu, Kali Linux, Raspberry Pi OS, Debian, and other Debian-based distributions, use the command given below −

sudo apt install vsftpd

To install it on Arch Linux, use the following command −

sudo pacman -S vsftpd

To install it on CentOS, use −

sudo yum install vsftpd

On Fedora, use the command given below −

sudo dnf install vsftpd

After the installation, start the service using the systemctl command −

sudo systemctl start vsftpd.service

To verify, check the status −

sudo systemctl status vsftpd.service
ftp Command in Linux1

Syntax of ftp Command

The syntax of the Linux ftp command is as follows −

ftp [options] [IP]

The [options] field is used to specify the various ftp command options. The [IP] field is used to specify the remote system IP address that is to be accessed.

Configuring FTP Server

After the installation, the FTP needs to be configured using the configuration file. The file can be accessed using any text editor.

sudo nano /etc/vsftpd.conf

Open the file and ensure anonymous_enable is set to NO and local_enable is YES. To enable modifications of remote system files, set write_enable to YES. Save the changes and exit the editor.

ftp Command in Linux2

Now restart the service to apply the changes −

sudo systemctl restart vsftpd.service

Enable the ports 20 and 21 from the firewall.

sudo ufw allow 20/tcp
sudo ufw allow 21/tcp

Options of ftp Command

The options of ftp command are listed below −

OptionsDescription
-ATo use active mode for transfer
-4To use only IPv4
-6To use only IPv6
-pTo use passive mode for transfer (default)
-iTo disable interactive prompts for batch transfer
-eTo disable command editing
-gTo disable file globbing
-mTo disable binding for same interface for passive transfer
-vTo enable verbose mode
-dTo enable debugging mode

FTP Interface Commands

The ftp command initiates a separate interface after connecting to the remote system. In the remote system, there is another set of sub-commands that are used for further operations. The sub-commands are listed below −

CommandsDescription
! [command [args]]To invoke the interactive shell on the local system
$ [macro-name [args]]To execute a defined macro
account [passwd]To supply a password for the remote system
appendTo append a local file on a file on the remote system
asciiTo set the file transfer type to network ASCII (default)
binaryTo set the file transfer type to binary image
bye/quitTo end the ftp session
cd [remote-directory]To change the directory on the remote system
chmod [mode] [file-name]To change the permissions on the remote system
close/disconnectTo close the ftp session
delete [remote-file]To delete a file on the remote system
dir [remote-directory] [local-file]To list the directory content on the remote system
get [remote-file] [local-file]To retrieve or download files from the remote system
hashTo print the # sign for each transferred data
helpTo display help
lcd [directory]To change the working directory on the local machine.
ls [remote-directory] [local-file]To list directory contents on the remote system
mls [remote-directory] [local-file]To list the content of multiple directories
mkdir [directory-name]To create a directory on the remote system
mdelete [remote-files]To delete multiple remote files
mget [remote-files]To retrieve multiple files from the remote machine
mput [local-files]To send multiple files to the remote machine
open [host] [port]To connect to an FTP server at a specified host and port
put [local-file] [remote-file]To send or upload a file to the remote system
pwdTo print the current working directory on the remote system
rename [from] [to]To rename a file on the remote system
rmdir [directory-name]To remove the directory on the remote system
statusTo show the status ofthe FTP session
user [user-name] [password] [account]To login to a FTP server

Examples of ftp Command in Linux

This section demonstrates the usage of the ftp command in Linux with examples −

Setting Up FTP Connection

Before transferring files, an FTP connection must be established with the remote system. To connect to a remote system, use the ftp command with the IP address of the remote system −

ftp 192.168.0.144

Before setting up the FTP interface, it will prompt for the username and password of the remote system. Once the credentials are added, the FTP connection will be established.

ftp Command in Linux3

Listing Directories

To list the content of the remote system, use the ls command. To list the contents of the current directory, use the following command −

ls
ftp Command in Linux4

To list the content of a specific directory, specify the path of the directory −

ls myDirectory

To list the content of multiple directories, use the mls command −

mls mydir1 mydir2

Changing Directory

To change the directory on the remote system, use the cd command with the directory name or path,

cd myDirectory
ftp Command in Linux5

To go back to the previous directory, use −

cd ..

Creating Directories

To create directories on the remote system, use the mkdir command with the directory name.

mkdir sampleDir
ftp Command in Linux6

Downloading a File

To download a file from the remote system, use the get command. For example, to download a file test.txt file from the remote machine, use the get command in the following way −

get test.txt

To download the file and save it with a different name on the local system, use the get command in the following way −

get test.txt sample.txt

The above command will download the test.txt file as a sample.txt file on the local system.

To download multiple files, use the mget command in the FTP session −

mget test1.txt test2.txt

Before downloading multiple files, the command will prompt with the [anpqy] option. Here is the breakdown of these options −

aAllApply the action to all subsequent files without prompting
nNoSkip the current action
pPromptAsk for confirmation on each action
qQuitCancel the operation
yYesProceed with the current action

Uploading a File

To upload a file from the local system to a remote system, use the put command −

put test.txt

To upload with a different name, use −

put test.txt sample.txt

The above command will upload the test.txt file as a sample.txt file on the remote system.

To upload multiple files, use the mput command −

mput test1.txt test2.txt

Before uploading multiple files, the command will prompt with the [anpqy] options.

Renaming a File

To rename a file on the remote system, use the rename command. For example, to rename the test.txt file to sample.txt, use the rename command as follows −

rename test.txt sample.txt
ftp Command in Linux7

Deleting a File

To delete a file on the remote system, use the delete command with the file name.

delete test.txt
ftp Command in Linux8

To delete multiple files, use the mdelete command −

mdelete test1.txt test2.txt
ftp Command in Linux9

Before deleting multiple files, the command will prompt with the [anpqy] options.

Closing the FTP Connection

To close the FTP connection, type the exit command and press Enter.

exit
ftp Command in Linux10

Conclusion

The ftp command in Linux is used to upload and download files from and to a remote system. The FTP is a widely used file transfer protocol that allows the transfer of files over the internet. The FTP traffic is not encrypted, it is always recommended to use it in a secure network.

In this tutorial, we explained the ftp command, its installation, syntax, options, and usage in Linux with examples.