ftp

networkingLinux/Unix/Windows
The ftp command is one of the most frequently used commands in Linux/Unix-like operating systems. ftp The ftp command is a standard network file transfer program that allows you to transfer files between your local machine and remote servers using the File Transfer Protocol (FTP). It provides both interactive and automated modes for transferring files, managing directories, and controlling file permissions on remote servers.

Quick Reference

Command Name:

ftp

Category:

networking

Platform:

Linux/Unix/Windows

Basic Usage:

ftp [options] [arguments]

Common Use Cases

  • 1

    Interactive file transfer

    Transfer files using an interactive command-line interface

  • 2

    Website management

    Upload and manage files on web hosting servers

  • 3

    Automated transfers

    Use script files to automate file transfer operations

  • 4

    Remote file management

    Create, delete, and organize files on remote FTP servers

Syntax

ftp [options] [host [port]]

Options

Option Description
-4 Use IPv4 addresses only
-6 Use IPv6 addresses only
-a Use anonymous login
-d Enable debugging mode
-e Disable command line editing
-i Turn off interactive prompting during multiple file transfers
-n Do not attempt auto-login upon initial connection
-p Use passive mode for data transfers
-s Enable secure data connection
-t Enable packet tracing
-u Specify username for connection
-v Show all responses from the remote server

Common Interactive Commands:

Command Description
ascii Set transfer mode to ASCII
binary Set transfer mode to binary
bye Exit FTP session
cd Change remote directory
delete Delete remote file
dir List remote directory contents (verbose)
get Download a file
help Display help information
lcd Change local directory
ls List remote directory contents
mget Download multiple files
mkdir Create remote directory
mput Upload multiple files
passive Toggle passive mode
prompt Toggle interactive prompting
put Upload a file
pwd Show current remote directory
quit Exit FTP session
rmdir Remove remote directory
status Show current status

Examples

How to Use These Examples

The examples below show common ways to use the ftp command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

#

Basic Examples:

ftp example.com

Connect to an FTP server and enter interactive mode.

ftp -u user@example.com

Connect to a server with a specified username.

ftp example.com 2121

Connect to a server on a non-standard port.

Interactive Mode Commands:

ls

List files in the current remote directory.

cd remote_directory

Change to a different directory on the remote server.

get remote_file [local_file]

Download a file from the remote server to your local machine.

mget *.txt

Download multiple files using wildcards.

put local_file [remote_file]

Upload a file from your local machine to the remote server.

mput *.jpg

Upload multiple files using wildcards.

binary

Switch to binary transfer mode for non-text files.

ascii

Switch to ASCII transfer mode for text files.

prompt

Toggle interactive prompting during multiple file transfers.

mkdir new_directory

Create a new directory on the remote server.

bye

Exit the FTP session.

Advanced Usage:

ftp -i example.com < commands.txt

Run FTP commands from a script file in non-interactive mode.

echo "user username password" > cmds.txt
echo "binary" >> cmds.txt
echo "get file.zip" >> cmds.txt
echo "bye" >> cmds.txt
ftp -n example.com < cmds.txt

Automate FTP transfers with a script file containing login credentials and commands.

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

Key Points:

  • FTP transfers data in plain text, including passwords, making it insecure for sensitive data
  • Consider using more secure alternatives like SFTP or FTPS for sensitive transfers
  • Binary mode should be used for non-text files (images, executables, compressed files, etc.)
  • ASCII mode is appropriate for text files (plain text, HTML, XML, etc.)
  • Passive mode is helpful when connecting from behind firewalls or NAT routers
  • The default port for FTP is 21, but many servers use alternate ports
  • Anonymous FTP allows access using "anonymous" as the username and your email as the password

Security Considerations:

  • FTP transmits usernames and passwords in plain text, making them vulnerable to interception
  • Consider using .netrc files for storing credentials but protect them with appropriate permissions
  • For sensitive data, use SFTP (SSH File Transfer Protocol) or FTPS (FTP Secure) instead
  • Avoid putting passwords in command line scripts, as they may be visible in process listings
  • Use caution when using anonymous FTP on untrusted servers
  • Always verify the server's identity before transferring sensitive data

File Transfer Modes:

  • ASCII mode: Converts line endings between different systems (e.g., Windows CRLF to Unix LF)
  • Binary mode: Performs exact byte-for-byte copy with no modifications
  • Default mode is usually ASCII, but binary mode should be used for most non-text files
  • Incorrect mode selection can corrupt files during transfer
  • Modern FTP clients often detect the appropriate mode automatically

Automation Tips:

  • Create script files with FTP commands for repetitive or scheduled tasks
  • Use the -i flag to disable interactive prompting in scripts
  • Consider using cron jobs to schedule regular file transfers
  • For complex automation, consider specialized tools like lftp or curl
  • Store credentials securely using environment variables or protected files

Common Issues:

  • Connection refused: Check server address, port, and firewall settings
  • Login failed: Verify username and password
  • Passive mode problems: Try toggling between active and passive modes
  • Transfer stalls: Network issues or firewall interference may be the cause
  • File corruption: Ensure you're using the correct transfer mode (ASCII vs. binary)
  • Permission denied: Check file permissions on both local and remote systems

Related Commands:

  • sftp - Secure File Transfer Protocol client (uses SSH for encryption)
  • scp - Secure copy (file transfer using SSH)
  • ncftp - Enhanced FTP client with bookmarks and additional features
  • lftp - Sophisticated file transfer program with additional protocols
  • curl - Tool for transferring data with URL syntax
  • wget - Non-interactive network downloader
  • rsync - Fast, versatile file copying tool

Tips & Tricks

1

Use binary mode for non-text files: Type "binary" before transferring images, executables or archives

2

Use passive mode behind firewalls: Type "passive" to switch to passive mode for better connectivity

3

Create batch scripts for automation: ftp -n server < commands.txt for unattended transfers

4

Use .netrc file for automatic logins: Create ~/.netrc with "machine ftp.example.com login user password pass"

5

Disable interactive prompts: ftp -i server to avoid confirmation for each file in mget/mput operations

6

Resume interrupted downloads: Use "reget" instead of "get" to continue partially downloaded files

7

Use mget/mput with wildcards: "mget *.txt" to download multiple files matching a pattern

8

Toggle verbose mode: Type "verbose" to see detailed transfer information for debugging

Common Use Cases

Interactive file transfer

Transfer files using an interactive command-line interface

Website management

Upload and manage files on web hosting servers

Automated transfers

Use script files to automate file transfer operations

Remote file management

Create, delete, and organize files on remote FTP servers

Legacy system access

Connect to older systems that only support the FTP protocol

Related Commands

sftp

sftp

View command

scp

scp

View command

rsync

rsync

View command

wget

wget

View command

curl

curl

View command

ssh

ssh

View command

nc

nc

View command

lftp

lftp

View command

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the ftp command works in different scenarios.

$ ftp
View All Commands