How to Use the Linux ftp Command

January 5, 2022

Introduction

FTP (File Transfer Protocol) is a network protocol used for transferring files from one computer system to another. Even though the safety of FTP tends to spark a lot of discussion, it is still an effective method of transferring files within a secure network.

In this tutorial, we will show you how to use the ftp command to connect to a remote system, transfer files, and manage files and directories.

How to use the ftp command in Linux

Prerequisites

IMPORTANT: FTP traffic is not encrypted and is thus considered unsafe. It is not recommended to transfer files over the Internet using FTP. To learn more about secure alternatives to FTP, have a look at our articles on SFTP and TSL vs. SSL.

Linux ftp Command Syntax

The Linux ftp command uses the following basic syntax:

ftp [options] [IP]

The IP is the IP address of the system you are connecting to.

The options available for the ftp command are:

FTP Command OptionsDescription
-4Use only IPv4.
-6Use only IPv6.
-eDisables command editing and history support.
-pUses passive mode for data transfers, allowing you to use FTP despite a firewall that might prevent it.
-iTurns off interactive prompting during multiple file transfers.
-nDisables auto-login attempts on initial connection.
-gDisables file name globbing.
-vEnables verbose output.
-dEnables debugging.

The ftp command connects you to a remote system and initiates the FTP interface. The FTP interface uses the following commands to manage and transfer files to the remote system:

CommandDescription
!Temporarily escape to the local shell.
$Execute a macro.
?Display help text.
accountSupply a password for the remote system.
appendAppend a local file to a file on the remote system.
asciiSet the file transfer type to network ASCII (default type).
bellEnable a sound alert after each transfer is complete.
binarySet the file transfer type to binary image transfer.
byeExit the FTP interface.
caseToggle upper/lower case sensitivity when ID mapping during the mget command.
cdChange the current working directory on the remote system.
cdupChange to the parent of the current working directory on the remote system.
chmodChange file permissions on the remote system.
closeExit the FTP interface.
crToggle carriage return stripping on ASCII file transfers.
debugToggle debugging mode.
deleteDelete a file from the remote system.
dirList the contents of a directory on the remote system.
disconnectTerminate the FTP session.
exitTerminate the FTP session and exit the FTP interface.
formSet the file transfer format.
getTransfer a file from the remote system to the local machine.
globToggle meta character expansion of local file names.
hashToggle displaying the hash sign ("#") for each transferred data block.
helpDisplay help text.
idleSet an inactivity timer for the remote system.
imageSet the file transfer type to binary image transfer.
ipanyAllow any type of IP address.
ipv4Only allow IPv4 addresses.
ipv6Only allow IPv6 addresses.
lcdChange the current working directory on the local machine.
lsList the contents of a directory on the remote system.
macdefDefine a macro.
mdeleteDelete multiple files on the remote system.
mdirList the contents of multiple directories on the remote system.
mgetTransfer multiple files from the remote system to the local machine.
mkdirCreate a directory on the remote system.
mlsList the contents of multiple directories on the remote system.
modeSet the file transfer mode.
modtimeShow the last time a file on the remote system was modified.
mputTransfer multiple files from the local machine to the remote system.
newerTransfer a file from the remote system to the local machine only if the modification time of the remote file is more recent than that of the local file (if a local version of the file doesn't exist, the remote file is automatically considered newer).
nlistList the contents of a directory on the remote system.
nmapSet templates for default file name mapping.
ntransSet translation table for default file name mapping.
openEstablish a connection with an FTP server.
passiveEnable passive transfer mode.
promptForce interactive prompts when transferring multiple files.
proxyExecute command on an alternate (proxy) connection.
putTransfer a file from the local machine to the remote system.
pwdDisplay the current working directory on the remote system.
qcToggle displaying a control character ("?") in the output of ASCII type commands.
quitTerminate the FTP session and exit the FTP interface.
quoteSpecify a command as an argument and send it to the FTP server.
recvTransfer a file from the remote system to the local machine.
regetTransfer a file from the remote system to the local machine if the local file is smaller than the remote file. The transfer starts at the end of the local file. If there is no local version of the file, the command doesn't execute.
renameRename a file on the remote system.
resetClear queued command replies.
restartRestart a file transfer command at a set marker.
rhelpDisplay help text for the remote system.
rmdirRemove a directory on the remote system.
rstatusShow the status of the remote system.
runiqueToggle storing files on the local machine with unique filenames.
sendTransfer a file from the local machine to the remote system.
sendportToggle the use of PORT commands.
siteSpecify a command as an argument and send it to the FTP server as a SITE command.
sizeDisplay the size of a file on the remote system.
statusShow the status of the FTP interface.
structSet the file transfer structure.
suniqueToggle storing files on the remote system with unique filenames.
systemShow the operating system on the remote system.
tenexSet the file transfer type to allow connecting to TENEX machines.
tickToggle printing byte counter during transfers.
traceToggle packet tracing.
typeSet a file transfer type.
umaskSet a default permissions mask for the local machine.
userProvide username and password for the remote FTP server.
verboseToggle verbose output.

How to Use ftp Command in Linux

The ftp command connects a computer system to a remote server using the FTP protocol. Once connected, it also lets users transfer files between the local machine and the remote system, and manage files and directories on the remote system.

Establish an FTP Connection

To establish an FTP connection to a remote system, use the ftp command with the remote system's IP address:

ftp [IP]

For instance, connecting to a remote server with the IP address 192.168.100.9:

ftp 192.168.100.9

Log into the FTP Server

Once you initiate a connection to a remote system using the ftp command, the FTP interface requires you to enter a username and password to log in:

Connecting with the ftp command requires you to enter login credentials

Entering the required credentials logs you in and starts the FTP interface. In this example, we are logging in as the phoenixnap user:

Entering the required login credentials

The FTP interface is now active and ready to execute commands:

Successfully logging in activates the FTP interface

Working with Directories on a Remote System

Using FTP, you can perform basic directory management on the remote system, such as creating directories, moving from one working directory to another, and listing directory contents.

List Directories

The FTP interface allows you to list the contents of a directory on a remote system using the ls command:

ls

Using the command without any options displays the content of the remote system's current working directory. In this example, that is the Home directory:

Using the ls command to list the content of a directory on the remote system

Note: The FTP interface allows you to use standard ls command options. Learn more in our guide to the Linux ls command.

Specifying the path to a directory as an argument to the ls command displays the content of that directory:

ls [path to directory]

For example, listing the content of the Example directory:

ls Example
Listing the content of a specific directory on the remote system

Appending the name of a text file to the end of the ls command saves the content of a directory to that file:

ls [path to directory] [file name]

For instance:

ls Example listing.txt

This command syntax requires you to type Y and press Enter to confirm saving the text file:

Saving the contents of a directory as a text file

Opening the text file reveals the content of the directory:

The contents of a directory as a text file

The dir and nlist commands are alternatives to the ls command and work the same way. The FTP interface also allows you to list the contents of multiple directories using the mls command:

mls [directory 1] [directory 2] .. [directory n]

For instance, the example below lists the contents of Example and Example2:

mls Example Example2 -
Listing the contents of multiple directories with the mls command

Like the ls command, the mls command allows users to save the contents in a text file. This command treats the last argument as the name of the text file. If you want to list directory contents without saving them to a text file, replace the file name with a dash symbol (-).

The mdir command works the same as the mls command but offers a more detailed output:

mdir Example Example2 -
Listing the contents of multiple directories with the mdir command

Change Directories

Use the cd command to change the current working directory on the remote system:

cd [path to directory]

For instance, moving to the Example directory:

cd Example
Changing the current working directory on the remote system

Use the cdup command to move to the parent of the current working directory. In this example, we are moving from the Example directory to the Home directory:

cdup
Changing to the parent of the current working directory

Create Directories

Using the mkdir command allows you to create a directory on the remote system:

mkdir [directory name]

In the example below, we create a directory named Example3:

mkdir Example3
Creating a new directory on the remote system

Download Files via FTP

To transfer a file from a remote system to the local machine, use the get or recv command.

get [remote file name]

OR

recv [remote file name]

In the example below, we transfer example_file.txt to the local machine.

get example_file.txt
Transferring a file from the remote system to the local machine

To transfer example_file.txt and save it as example.txt on the local machine, use:

get example_file.txt example.txt
Transferring and renaming a file from the remote system to the local machine

Transferring a file from a specific directory requires you to move into that directory:

cd Example
get test01.txt
Transferring a file from a directory on the remote system to the local machine

The mget command allows you to transfer multiple files at the same time. For example, transferring test01.txt, test02.txt, and test03.txt from the Example directory:

mget test01.txt test02.txt test03.txt
Transferring multiple files from the remote system to the local machine

Note: By default, the mget command displays an interactive prompt asking users to confirm each file transfer. Use the prompt command to toggle this feature on and off.

Upload Files via FTP

Use the put or send command to transfer a file from the local machine to a remote system. Both commands use the same basic syntax:

put [local file name]
send [local file name]

To transfer example01.txt to the remote system, use:

put example01.txt
Transferring a file from the local machine to the remote system

To upload example01.txt to the remote system as sample01.txt, use:

put example01.txt sample01.txt
Transferring and renaming a file from the local machine to the remote system

Moving into a specific directory allows you to transfer files from that directory:

cd Directory
put example.txt
Transferring a file from a directory on the local machine to the remote system

Use the mput command to transfer multiple files to the remote system. For example, transfer test04.txt, test05.txt, and test06.txt with:

mput test04.txt test05.txt test06.txt
Transferring multiple files from the local machine to the remote system

Note: Using the mput command creates an interactive prompt asking the user to confirm each file transfer. Use the prompt command to toggle this feature on and off.

Rename Files

Use the rename command to rename files on the remote server. The rename command uses the following syntax:

rename [old file name] [new file name]

For instance, renaming sample01.txt to sample_file01.txt:

rename sample01.txt sample_file01.txt

Executing the command successfully produces the following output:

Renaming a file on the remote system

Use the rename command to change directory names too.

In the example below, the Example3 directory is renamed to Example03:

rename Example3 Example03
Renaming a directory on the remote system

Delete Files

The delete command allows you to delete a file on the remote system. It uses the following syntax:

delete [file name]

For instance, deleting sample_file01.txt:

delete sample_file01.txt
Deleting a file on the remote system

Using the mdelete command allows you to delete multiple files at the same time by adding the file names after the command:

mdelete test04.txt test05.txt test06.txt
Deleting multiple files from the remote system using file names

Note: When using the mdelete command, an interactive prompt asks you to confirm each file deletion. Use the prompt command to toggle this feature on and off.

Another method is to use the mdelete command with a wildcard character. For instance, to delete all .txt files, use:

mdelete *.txt
Deleting multiple files from the remote system using a wildcard character

Close the FTP Connection

Use the bye, exit, or quit command to terminate the FTP connection and exit the interface.

Using the disconnect command closes the connection without exiting the interface.

Terminating the FTP connection

Conclusion

After reading this article, you should be able to establish an FTP connection between a local system and a remote server, and use it to transfer files and perform basic file and directory management.

If you are interested in transferring files over the Internet, learn more about SFTP commands, a safer alternative to FTP.

Was this article helpful?
YesNo
Aleksandar Kovačević
With a background in both design and writing, Aleksandar Kovacevic aims to bring a fresh perspective to writing for IT, making complicated concepts easy to understand and approach.
Next you should read
How Does SFTP Work?
November 18, 2021

SFTP is a component of SSH that allows users to safely transfer data using the Internet. This tutorial goes over how SFTP works and what makes it the right choice of transfer protocols.
Read more
How to Use SFTP to Transfer Files
November 18, 2021

SFTP uses SSH encryption to help you securely transfer files and directories between remote systems. This tutorial shows you how to use SFTP to transfer files in the terminal interface.
Read more
403 Forbidden Error - What Is It and How to Fix It
October 7, 2021

The 403 Forbidden error appears when you are trying to access content, but the access is denied. This article deals with different ways of fixing the 403 error.
Read more
What is SSH?
August 14, 2024

The SSH protocol offers system administrators a way to establish a secure connection that will protect them against malicious cyber-attacks, such as password-sniffing.
Read more