ftp
Overview
The ftp
command is a file transfer protocol client used to transfer files between local and remote systems. While largely superseded by more secure alternatives, it’s still used for legacy systems and public file servers.
Syntax
ftp [options] [host]
Common Options
Option | Description |
---|---|
-4 |
Use IPv4 only |
-6 |
Use IPv6 only |
-A |
Force active mode |
-a |
Use anonymous login |
-d |
Enable debugging |
-e |
Disable command editing |
-g |
Disable filename globbing |
-i |
Turn off interactive prompting |
-n |
No auto-login |
-p |
Use passive mode |
-v |
Verbose output |
Key Use Cases
- File transfer to/from FTP servers
- Legacy system integration
- Public file downloads
- Automated file transfers
- System administration
Examples with Explanations
Example 1: Connect to FTP Server
ftp ftp.example.com
Connects to FTP server and prompts for credentials
Example 2: Anonymous FTP
ftp -a ftp.example.com
Connects using anonymous login
Example 3: Non-interactive Mode
ftp -n ftp.example.com
Connects without automatic login
FTP Commands
Command | Description |
---|---|
ls |
List remote files |
cd |
Change remote directory |
lcd |
Change local directory |
pwd |
Show remote directory |
lpwd |
Show local directory |
get |
Download file |
put |
Upload file |
mget |
Download multiple files |
mput |
Upload multiple files |
binary |
Set binary transfer mode |
ascii |
Set ASCII transfer mode |
passive |
Toggle passive mode |
quit |
Exit FTP |
File Transfer Examples
Download Files
ftp> get filename.txt
ftp> mget *.txt
ftp> get remote.txt local.txt
Upload Files
ftp> put filename.txt
ftp> mput *.txt
ftp> put local.txt remote.txt
Transfer Modes
Mode | Description |
---|---|
ASCII | Text files (default) |
Binary | Binary files |
Auto | Automatic detection |
Set transfer mode:
ftp> binary
ftp> ascii
Common Usage Patterns
Batch download:
ftp> mget *.log
Directory synchronization:
ftp> lcd /local/path ftp> cd /remote/path ftp> mget *
Automated transfer:
ftp> prompt off ftp> mput *.txt
Passive vs Active Mode
- Active Mode: Server connects back to client
- Passive Mode: Client connects to server for data
Enable passive mode:
ftp> passive
Scripting FTP Operations
Using here document:
ftp -n ftp.example.com << EOF user username password binary cd /remote/path lcd /local/path mget *.txt quit EOF
Using command file:
echo "user username password binary get file.txt quit" > ftp_commands.txt ftp -n ftp.example.com < ftp_commands.txt
Security Considerations
- Unencrypted protocol
- Credentials sent in plain text
- Data transmitted unencrypted
- Use SFTP/SCP for secure transfers
- Firewall configuration needed
Best Practices
- Use secure alternatives when possible
- Use passive mode for firewalls
- Set appropriate transfer modes
- Verify file transfers
- Use automation for repetitive tasks
Automated FTP Scripts
Backup script:
#!/bin/bash HOST="backup.server.com" USER="backup_user" PASS="backup_pass" ftp -n $HOST << EOF user $USER $PASS binary cd /backups lcd /local/backups mput *.tar.gz quit EOF
Download script:
#!/bin/bash ftp -n ftp.example.com << EOF user anonymous anonymous@domain.com binary cd /pub/files mget *.zip quit EOF
Error Handling
Connection errors:
ftp -v ftp.server.com 2>&1 | grep -i error
Transfer verification:
ftp> hash # Show progress ftp> status # Show connection status
Performance Optimization
- Use binary mode for non-text files
- Enable hash marks for progress
- Use passive mode for better connectivity
- Consider parallel transfers for multiple files
Troubleshooting
- Connection refused
- Login failures
- Transfer mode issues
- Firewall problems
- Permission errors
Modern Alternatives
Instead of FTP, consider: 1. sftp
- Secure FTP over SSH 2. scp
- Secure copy over SSH 3. rsync
- Efficient file synchronization 4. curl
- Modern data transfer 5. wget
- Web-based downloads
Integration Examples
Log rotation upload:
# Upload rotated logs find /var/log -name "*.gz" -mtime -1 | while read file; do echo "put $file" | ftp -n backup.server.com done
Configuration deployment:
ftp -n config.server.com << EOF user deploy deploy_pass ascii cd /configs mput *.conf quit EOF