Comprehensive Guide to the FTP Command in Linux

Comprehensive Guide to the FTP Command in Linux

The ftp command in Linux is a standard client for transferring files to and from remote servers using the File Transfer Protocol (FTP). It’s a versatile tool for uploading, downloading, and managing files on FTP servers, commonly used for website maintenance, backups, and data sharing. This guide provides a comprehensive overview of the ftp command, covering its syntax, options, interactive commands, and practical examples, tailored for both beginners and advanced users as of August 15, 2025. The information is based on the latest GNU ftp (from inetutils 2.5) and common Linux distributions like Ubuntu 24.04, with considerations for secure alternatives like SFTP.

What is the ftp Command?

The ftp command initiates an interactive session to connect to an FTP server, allowing users to:

  • Upload and download files.
  • Navigate remote and local directories.
  • Manage files (e.g., delete, rename).
  • Automate transfers in scripts.

Note: FTP is inherently insecure as it transmits data (including credentials) in plain text. For secure transfers, consider SFTP (sftp) or FTPS, which are covered briefly at the end.

Prerequisites

  • Operating System: Linux (e.g., Ubuntu 24.04), macOS, or Unix-like system.
  • Access: ftp installed (part of inetutils, pre-installed on many distributions).
  • Permissions: Access to an FTP server with valid credentials (username and password).
  • Network: Open port 21 (FTP control) and 20 (data, for active mode) or a range for passive mode.
  • Optional: Knowledge of FTP server details (e.g., hostname, port).

Verify ftp installation:

ftp --version

Install if missing (Ubuntu/Debian):

sudo apt-get update
sudo apt-get install -y inetutils-ftp

Syntax of the ftp Command

The general syntax is:

ftp [OPTIONS] [HOST]
  • OPTIONS: Command-line flags to modify behavior.
  • HOST: The FTP server’s hostname or IP address (e.g., ftp.example.com or 192.168.1.100).

If HOST is omitted, you enter interactive mode and can connect later.

Common Command-Line Options

Below are key ftp command-line options (from man ftp, GNU inetutils 2.5):

OptionDescription
-vVerbose mode: show detailed responses from the server.
-nSuppress auto-login; requires manual user command.
-iDisable interactive prompting during multiple file transfers (useful for scripts).
-pEnable passive mode (default in modern clients; better for firewalls).
-dEnable debugging output for troubleshooting.
-gDisable filename globbing (wildcards like *).
--helpDisplay help information.
--versionShow version information.

Interactive FTP Commands

Once connected to an FTP server, you interact via commands. Below are the most common:

CommandDescription
open HOST [PORT]Connect to the specified host and port (default: 21).
user USER [PASS]Log in with username and optional password.
ls [DIR]List files in the remote directory.
dir [DIR]Detailed directory listing (like ls -l).
cd DIRChange remote directory.
lcd DIRChange local directory.
get FILE [LOCAL]Download a file to the local system.
put FILE [REMOTE]Upload a file to the remote server.
mget FILESDownload multiple files (supports wildcards, e.g., *.txt).
mput FILESUpload multiple files (supports wildcards).
delete FILEDelete a file on the remote server.
mdelete FILESDelete multiple files (supports wildcards).
mkdir DIRCreate a remote directory.
rmdir DIRRemove a remote directory.
pwdPrint the current remote working directory.
binarySet binary transfer mode (for non-text files, e.g., images).
asciiSet ASCII transfer mode (for text files).
promptToggle interactive prompting for multiple file transfers.
statusShow current settings (e.g., mode, verbosity).
closeClose the connection to the current server.
quitExit the FTP session.
!COMMANDRun a local shell command (e.g., !ls).
help [COMMAND]Display help for a specific command or list all commands.

Practical Examples

Below are step-by-step examples for common FTP tasks, assuming an FTP server at ftp.example.com with username user and password pass.

1. Connect to an FTP Server

Start an FTP session:

ftp ftp.example.com

Output:

Connected to ftp.example.com.
220 Welcome to Example FTP Server
Name (ftp.example.com:user): user
331 Please specify the password.
Password: pass
230 Login successful.
ftp>

2. Connect Without Auto-Login

Use -n to suppress auto-login:

ftp -n ftp.example.com
ftp> user user pass

3. List Remote Directory Contents

List files:

ftp> ls

Output:

200 PORT command successful.
150 Opening ASCII mode data connection.
file1.txt
image.jpg
backup.tar.gz
226 Transfer complete.

Detailed listing:

ftp> dir

4. Download a File

Download file1.txt to the local directory:

ftp> get file1.txt

Download to a specific local file:

ftp> get file1.txt /home/user/downloads/file1.txt

5. Upload a File

Upload localfile.txt to the remote server:

ftp> put localfile.txt

Upload to a specific remote path:

ftp> put localfile.txt /remote/path/file.txt

6. Download Multiple Files

Download all .txt files:

ftp> mget *.txt

Disable prompting for automation:

ftp> prompt
Interactive mode off.
ftp> mget *.txt

7. Upload Multiple Files

Upload all .jpg files:

ftp> mput *.jpg

8. Set Transfer Mode

For binary files (e.g., images, archives):

ftp> binary
200 Type set to I.

For text files:

ftp> ascii
200 Type set to A.

9. Navigate Directories

Change remote directory:

ftp> cd /remote/path

Change local directory:

ftp> lcd /home/user/downloads

10. Create and Delete Remote Directories

Create a directory:

ftp> mkdir backups

Remove a directory (must be empty):

ftp> rmdir backups

11. Delete Files

Delete a single file:

ftp> delete file1.txt

Delete multiple files:

ftp> mdelete *.bak

12. Automate FTP in a Script

Create a script (ftp_upload.sh):

#!/bin/bash
HOST="ftp.example.com"
USER="user"
PASS="pass"
ftp -n $HOST <<EOF
user $USER $PASS
binary
cd /remote/path
put /home/user/localfile.txt
quit
EOF

Run it:

chmod +x ftp_upload.sh
./ftp_upload.sh

13. Use Passive Mode

Enable passive mode for firewall compatibility:

ftp -p ftp.example.com

Or in interactive mode:

ftp> passive
Passive mode on.

14. Run Local Commands

List local files during an FTP session:

ftp> !ls

15. Download with Verbose Output

Enable verbose mode for details:

ftp -v ftp.example.com
ftp> get file1.txt

Advanced Use Cases

  • Batch File Transfers:
    Create a batch file (commands.ftp):
  user user pass
  binary
  cd /remote/path
  mput *.jpg
  quit

Run:

  ftp -n ftp.example.com < commands.ftp
  • Sync with FTP Server:
    Use rsync over FTP (if supported) for incremental sync:
  rsync -av --progress /local/path/ ftp://user:[email protected]/remote/path/
  • Monitor Transfer Progress:
    Use verbose mode or pipe to pv (if installed):
  ftp ftp.example.com | pv
  • Automate with .netrc:
    Create ~/.netrc for auto-login:
  machine ftp.example.com
  login user
  password pass

Secure it:

  chmod 600 ~/.netrc

Connect without credentials:

  ftp ftp.example.com

Troubleshooting Common Issues

  • “Connection Refused”:
  • Ensure port 21 is open:
    bash telnet ftp.example.com 21
  • Check server status or firewall settings.
  • “Login Incorrect”:
  • Verify username and password.
  • Use -n and manual user command to test: ftp -n ftp.example.com ftp> user user pass
  • “Passive Mode Issues”:
  • Enable passive mode (-p or passive command).
  • Check firewall for passive port range (usually 1024–65535).
  • Slow Transfers:
  • Switch to binary mode for non-text files:
    bash ftp> binary
  • Test network speed: ping ftp.example.com
  • File Corruption:
  • Ensure correct transfer mode (binary for images/archives, ascii for text).
  • Retry with verbose output (-v) to diagnose.
  • Script Failures:
  • Add error handling:
    bash ftp -n ftp.example.com <<EOF user user pass binary put localfile.txt || echo "Upload failed" quit EOF

Security Considerations

  • Insecure Protocol: FTP sends credentials and data in plain text. Use SFTP or FTPS for security.
  • Password Storage: Avoid hardcoding credentials in scripts; use .netrc with chmod 600.
  • Access Control: Restrict FTP server permissions to specific directories.
  • Firewall: Use passive mode (-p) to minimize open ports.

Alternatives to FTP

  • SFTP (Secure File Transfer Protocol):
    Uses SSH for encrypted transfers:
  sftp [email protected]

Commands are similar to ftp (e.g., get, put, ls).

  • SCP:
    Securely copy files over SSH:
  scp localfile.txt [email protected]:/remote/path/
  • rsync:
    Incremental transfers over SSH:
  rsync -avh -e 'ssh' /local/path/ [email protected]:/remote/path/
  • FTPS:
    FTP with SSL/TLS encryption (requires server support):
  ftp -p ftp.example.com
  • GUI Clients: FileZilla, WinSCP for user-friendly interfaces.

Conclusion

The ftp command is a lightweight, flexible tool for file transfers, suitable for managing files on remote servers. Its interactive commands (get, put, mget) and scripting capabilities make it versatile, though its lack of encryption necessitates caution. For secure alternatives, SFTP or rsync over SSH are recommended. By mastering ftp’s options and combining it with automation, you can streamline file transfers for backups, website updates, or data sharing. For further details, consult man ftp or info inetutils ftp, and test commands in a safe environment.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *