cURL is a multi-protocol data transfer tool supporting over 20 protocols. Understanding these protocol capabilities is important for choosing the appropriate transfer method.
Protocols Supported by cURL
| Protocol | Purpose | Example URL |
|---|---|---|
| HTTP/HTTPS | Web requests | http://example.com |
| FTP/FTPS | File transfer | ftp://ftp.example.com |
| SFTP | Secure file transfer | sftp://user@host.com |
| SCP | Secure copy | scp://user@host.com/file |
| TFTP | Simple file transfer | tftp://host.com/file |
| LDAP/LDAPS | Directory services | ldap://ldap.example.com |
| SMTP/SMTPS | Email sending | smtp://mail.example.com |
| POP3/POP3S | Email receiving | pop3://mail.example.com |
| IMAP/IMAPS | Email access | imap://mail.example.com |
| RTSP/RTMP | Streaming media | rtsp://stream.example.com |
| FILE | Local files | file:///path/to/file |
| GOPHER | Early internet protocol | gopher://gopher.example.com |
| DICT | Dictionary protocol | dict://dict.org/d:word |
| TELNET | Remote login | telnet://host.com |
FTP File Transfer
FTP Download
bash# Anonymous FTP download curl ftp://ftp.example.com/pub/file.txt -o file.txt # Authenticated FTP download curl -u "username:password" \ ftp://ftp.example.com/remote/file.txt \ -o file.txt # Download directory listing curl ftp://ftp.example.com/pub/ # Passive mode FTP curl --ftp-skip-pasv-ip \ ftp://ftp.example.com/file.txt
FTP Upload
bash# Upload file to FTP curl -u "username:password" \ -T local-file.txt \ ftp://ftp.example.com/remote/path/ # Upload with specific filename curl -u "username:password" \ -T local-file.txt \ ftp://ftp.example.com/remote/renamed-file.txt # Create directory and upload curl -u "username:password" \ --ftp-create-dirs \ -T file.txt \ ftp://ftp.example.com/new-folder/file.txt
Advanced FTP Operations
bash# Delete remote file curl -u "username:password" \ -Q "DELE remote-file.txt" \ ftp://ftp.example.com/ # Rename file curl -u "username:password" \ -Q "RNFR old-name.txt" \ -Q "RNTO new-name.txt" \ ftp://ftp.example.com/ # List detailed file info curl -u "username:password" \ ftp://ftp.example.com/ -l # Use FTPS (FTP over SSL) curl -u "username:password" \ ftps://ftp.example.com/file.txt # Active mode FTP curl --ftp-port - \ -u "username:password" \ ftp://ftp.example.com/file.txt
SFTP File Transfer
SFTP Download
bash# Basic SFTP download curl -u "username:password" \ sftp://sftp.example.com/remote/file.txt \ -o file.txt # Use key authentication (recommended) curl -u "username:" \ --key ~/.ssh/id_rsa \ --pubkey ~/.ssh/id_rsa.pub \ sftp://sftp.example.com/remote/file.txt \ -o file.txt # Specify port curl -u "username:password" \ sftp://sftp.example.com:2222/remote/file.txt # Download directory (requires tar) ssh user@sftp.example.com "tar czf - /remote/folder" | tar xzf -
SFTP Upload
bash# Upload file to SFTP curl -u "username:password" \ -T local-file.txt \ sftp://sftp.example.com/remote/path/ # Use key for upload curl -u "username:" \ --key ~/.ssh/id_rsa \ -T local-file.txt \ sftp://sftp.example.com/remote/ # Upload multiple files curl -u "username:password" \ -T "file1.txt" \ -T "file2.txt" \ sftp://sftp.example.com/remote/
SCP File Transfer
bash# SCP download curl -u "username:password" \ scp://host.example.com/remote/file.txt \ -o file.txt # SCP upload curl -u "username:password" \ -T local-file.txt \ scp://host.example.com/remote/ # Use key curl -u "username:" \ --key ~/.ssh/id_rsa \ scp://host.example.com/remote/file.txt
Protocol Comparison
| Feature | FTP | FTPS | SFTP | SCP |
|---|---|---|---|---|
| Encryption | ❌ | ✅ | ✅ | ✅ |
| Port | 21 | 21/990 | 22 | 22 |
| Authentication | Password | Password/Cert | Password/Key | Password/Key |
| Firewall Friendly | ❌ | ⚠️ | ✅ | ✅ |
| Recommended For | Internal | Enterprise | General | Simple transfer |
Email Protocol Examples
bash# SMTP send email curl -v smtp://smtp.gmail.com:587 \ --mail-from "sender@example.com" \ --mail-rcpt "recipient@example.com" \ --upload-file email.txt \ --user "username:password" # POP3 read email curl -u "username:password" \ pop3://pop.gmail.com # IMAP list mailboxes curl -u "username:password" \ imap://imap.gmail.com
Utility Script
bash#!/bin/bash # FTP/SFTP automatic backup script SOURCE="/local/path/to/backup" DEST="sftp://backup.example.com/backups/" USER="backup_user" KEY="~/.ssh/backup_key" DATE=$(date +%Y%m%d) ARCHIVE="backup-${DATE}.tar.gz" # Create archive tar czf "/tmp/${ARCHIVE}" -C "$(dirname $SOURCE)" "$(basename $SOURCE)" # Upload to SFTP curl -u "${USER}:" \ --key "$KEY" \ -T "/tmp/${ARCHIVE}" \ "${DEST}${ARCHIVE}" # Clean up local temp file rm "/tmp/${ARCHIVE}" echo "Backup completed: ${ARCHIVE}"
Common Issues
bash# Issue 1: FTP passive mode fails # Solution: Use --ftp-skip-pasv-ip curl --ftp-skip-pasv-ip ftp://ftp.example.com/file # Issue 2: SFTP key permission error # Solution: Ensure private key permissions are correct chmod 600 ~/.ssh/id_rsa # Issue 3: SSL certificate verification fails # Solution: Specify CA or skip verification (testing only) curl --cacert /path/to/ca.crt ftps://ftp.example.com/file curl -k ftps://ftp.example.com/file # Not secure # Issue 4: Filename contains spaces # Solution: Use URL encoding or quotes curl -u "user:pass" "ftp://ftp.example.com/file%20name.txt"