乐闻世界logo
搜索文章和话题

What protocols does cURL support? How to use FTP/SFTP for file transfer?

3月6日 23:06

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

ProtocolPurposeExample URL
HTTP/HTTPSWeb requestshttp://example.com
FTP/FTPSFile transferftp://ftp.example.com
SFTPSecure file transfersftp://user@host.com
SCPSecure copyscp://user@host.com/file
TFTPSimple file transfertftp://host.com/file
LDAP/LDAPSDirectory servicesldap://ldap.example.com
SMTP/SMTPSEmail sendingsmtp://mail.example.com
POP3/POP3SEmail receivingpop3://mail.example.com
IMAP/IMAPSEmail accessimap://mail.example.com
RTSP/RTMPStreaming mediartsp://stream.example.com
FILELocal filesfile:///path/to/file
GOPHEREarly internet protocolgopher://gopher.example.com
DICTDictionary protocoldict://dict.org/d:word
TELNETRemote logintelnet://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

FeatureFTPFTPSSFTPSCP
Encryption
Port2121/9902222
AuthenticationPasswordPassword/CertPassword/KeyPassword/Key
Firewall Friendly⚠️
Recommended ForInternalEnterpriseGeneralSimple 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"

标签:cURL