Linux 下载神器 wget 与 curl 命令详解 📥
🌐 掌握Linux下最强大的网络传输工具,轻松应对各种下载和数据传输需求
✨ 工具特点
⚡ 高效传输 : 支持多协议、高速下载和上传
🔄 断点续传 : 网络中断后可从断点继续传输
🛡️ 安全可靠 : 支持 SSL/TLS 加密和多种认证方式
📊 灵活可控 : 丰富的参数选项满足各种需求
🐧 跨平台 : 在几乎所有 Linux 发行版和 Unix 系统上可用
📖 目录导航
🔧 安装方法 🐧 Ubuntu/Debian 系统 1 2 3 4 5 6 7 8 9 10 11 12 sudo apt updatesudo apt install wget -ysudo apt install curl -ywget --version curl --version
🔴 CentOS/RHEL 系统 1 2 3 4 5 sudo yum install wget curl -ysudo dnf install wget curl -y
🍎 macOS 系统 1 2 3 4 5 brew install wget curl sudo port install wget curl
📦 源码编译安装(高级用户) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 wget https://ftp.gnu.org/gnu/wget/wget-latest.tar.gz tar -xzf wget-latest.tar.gz cd wget-*./configure --prefix=/usr/local --with-ssl=openssl make sudo make installwget https://curl.se/download/curl-7.88.1.tar.gz tar -xzf curl-7.88.1.tar.gz cd curl-7.88.1./configure --prefix=/usr/local --with-openssl make sudo make install
📥 wget 使用详解 🎯 基本下载操作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 wget https://example.com/file.zip wget -O custom_name.zip https://example.com/file.zip wget -q https://example.com/file.zip wget -v https://example.com/file.zip wget --tries=3 https://example.com/file.zip
⏯️ 断点续传与后台下载 1 2 3 4 5 6 7 8 9 10 11 12 13 14 wget -c https://example.com/large_file.iso wget -b https://example.com/large_file.iso tail -f wget-logwget -T 30 https://example.com/file.zip wget -w 5 https://example.com/file.zip
🚦 网络控制 1 2 3 4 5 6 7 8 9 10 11 wget --limit-rate=100k https://example.com/large_file.iso wget --limit-rate=1m https://example.com/large_file.iso wget --no-cache https://example.com/file.html wget --passive-ftp ftp://example.com/file.zip
🌐 递归下载与网站镜像 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 wget -r -l 5 https://example.com/ wget -r -l 5 -np https://example.com/path/ wget -r -A "*.jpg,*.png" https://example.com/images/ wget -r -R "*.tmp,*.bak" https://example.com/ wget -mk -w 2 https://example.com/ wget -k -r https://example.com/
🔐 认证与安全 1 2 3 4 5 6 7 8 9 10 11 wget --ftp-user=username --ftp-password=password ftp://example.com/file.zip wget --user=username --password=password https://example.com/protected/file.zip wget --no-check-certificate https://example.com/secure-file.zip wget --secure-protocol=TLSv1_2 https://example.com/secure-file.zip
📋 批量下载与管理 1 2 3 4 5 6 7 8 9 10 11 12 13 14 wget -i download_list.txt wget https://example.com/images/{1..10}.jpg wget -Q100m -i download_list.txt wget -P /path/to/download https://example.com/file.zip wget -o download.log https://example.com/file.zip
🔧 高级选项 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 wget -e use_proxy=yes -e http_proxy=proxy:port https://example.com wget -U "Mozilla/5.0" https://example.com wget --max-redirect=5 https://example.com/redirect wget --no-directories https://example.com/files/ wget -4 https://example.com wget -6 https://example.com
📡 curl 使用详解 🎯 基本请求操作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 curl https://example.com curl -o output.html https://example.com curl -O https://example.com/file.zip curl -s -O https://example.com/file.zip curl -v https://example.com curl -L https://example.com/redirect
📨 HTTP 方法操作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 curl https://api.example.com/users curl -X POST https://api.example.com/users curl -X POST -d "name=John&age=30" https://api.example.com/users curl -X PUT -d "name=John" https://api.example.com/users/1 curl -X DELETE https://api.example.com/users/1 curl -X PATCH -d "age=31" https://api.example.com/users/1 curl -I https://example.com curl -X OPTIONS https://api.example.com/users
📤 数据发送与处理 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 curl -d "param1=value1¶m2=value2" https://example.com/form curl -H "Content-Type: application/json" -d '{"key":"value"}' https://api.example.com/data curl -d @data.json https://api.example.com/data curl -F "name=John" -F "photo=@photo.jpg" https://example.com/upload curl --data-urlencode "name=John Doe" https://example.com/submit curl --raw https://example.com/data curl --compress -d @large_data.json https://api.example.com/data
📝 请求头管理 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 curl -H "Authorization: Bearer token123" https://api.example.com/protected curl -H "Content-Type: application/json" -H "X-Custom-Header: value" https://api.example.com/endpoint curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" https://example.com curl -e "https://google.com" https://example.com curl -H "Host:" https://example.com curl -D headers.txt https://example.com
🔐 认证与安全 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 curl -u username:password https://example.com/protected curl -u username https://example.com/protected curl -H "Authorization: Bearer your_token" https://api.example.com/data curl --digest -u username:password https://example.com/protected curl --ntlm -u username:password https://example.com/protected curl -k https://example.com/secure curl --cert client.pem --key key.pem https://example.com/secure curl --cacert ca-bundle.pem https://example.com/secure
🐛 调试与监控 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 curl -v https://example.com curl -I https://example.com curl -i https://example.com curl -w "Status: %{http_code}\nSize: %{size_download}\nTime: %{time_total}\n" https://example.com curl -w @format.txt https://example.com curl -L --max-redirs 5 https://example.com/redirect curl -# -O https://example.com/largefile.zip curl --limit-rate 100k -O https://example.com/largefile.zip
📊 高级功能 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 curl --parallel --parallel-max 3 -O https://example.com/file1.zip -O https://example.com/file2.zip curl -C - -O https://example.com/largefile.zip curl --socks5 proxy:port https://example.com curl -x http://proxy:port https://example.com curl -c cookies.txt https://example.com/login curl -b cookies.txt https://example.com/dashboard curl -T localfile.txt https://example.com/upload curl -T "{file1,file2}" https://example.com/upload curl -u user:pass ftp://example.com/ -O curl -Q "CWD /remote/directory" ftp://example.com/ curl --mail-from sender@example.com --mail-rcpt receiver@example.com --upload-file email.txt smtp://smtp.example.com
⚖️ 工具对比 📊 功能对比表
特性
🔍 wget
📡 curl
设计目的
文件下载
数据传输
递归下载
✅ 支持
❌ 不支持
输出默认
保存到文件
输出到终端
协议支持
HTTP, HTTPS, FTP
30+ 种协议
交互性
非交互式
可交互
断点续传
✅ 内置支持
✅ 需要参数
后台运行
✅ 支持
❌ 不支持
网站镜像
✅ 优秀支持
❌ 不支持
API 测试
❌ 有限支持
✅ 优秀支持
文件上传
❌ 不支持
✅ 优秀支持
进度显示
✅ 内置
✅ 需要参数
脚本友好
✅ 优秀
✅ 优秀
🎯 选择指南 选择 wget 当您需要:
📥 下载文件或整个网站
🔄 断点续传大型文件
🌐 递归下载网站内容
⏯️ 后台下载任务
📁 简单的文件下载操作
选择 curl 当您需要:
🔄 API 测试和调试
📤 文件上传功能
🛡️ 复杂的认证处理
📊 详细的调试信息
🔗 多种协议支持
📝 灵活的请求定制
🎯 应用场景 🌐 wget 实用场景 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 wget -c https://example.com/large_file.iso wget -mk -w 2 -np https://example.com/ wget -i url_list.txt -P /path/to/downloads 0 0 * * * wget -q -O /backups/update.zip https://example.com/daily_update.zip wget -r -A "*.pdf" https://example.com/documents/ wget --limit-rate=500k https://example.com/largefile.zip
🔄 curl 实用场景 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer token" -d '{"user":"test"}' https://api.example.com/users curl -F "file=@localfile.txt" -F "description=test file" https://example.com/upload curl -s -o /dev/null -w "%{http_code}\n" https://example.com curl "wttr.in/Beijing?format=3" curl -w "DNS: %{time_namelookup} Connect: %{time_connect} Start: %{time_starttransfer} Total: %{time_total}\n" https://api.example.com curl -u client_id:client_secret -d "grant_type=client_credentials" https://oauth.example.com/token curl --include --no-buffer --header "Connection: Upgrade" --header "Upgrade: websocket" --header "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==" --header "Sec-WebSocket-Version: 13" https://example.com/ws
🔗 组合使用场景 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 curl -L https://example.com/archive.tar.gz | tar xz curl -sSL https://example.com/install.sh | bash -s -- --option value for url in https://site1.com https://site2.com https://site3.com; do echo -n "$url : " curl -s -o /dev/null -w "%{http_code}" $url echo done cat requests.txt | while read url; do curl -X POST -H "Content-Type: application/json" -d @data.json "$url " done curl -O https://example.com/file.zip && echo "Download complete" || echo "Download failed"
💡 高级技巧 🎨 输出美化与处理 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 curl -s https://example.com/text | lolcat curl -s https://api.example.com/data | jq . curl -s https://api.example.com/data | xmllint --format - curl -s https://example.com/data.csv | csvtool readable - curl -s https://example.com/page.html | grep -o '<title>.*</title>' curl -s https://logs.example.com/stream | while read line; do echo "[$(date) ] $line " done
🔄 使用代理和隧道 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 curl -x http://proxy:8080 https://example.com curl --socks5 proxy:1080 https://example.com curl -x http://user:pass@proxy:8080 https://example.com ssh -L 8080:example.com:80 user@server curl http://localhost:8080 curl --proxy-auto-detect https://example.com curl --noproxy "localhost,127.0.0.1,.example.com" https://example.com
📊 性能优化技巧 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 cat url_list.txt | xargs -P 4 -I {} wget {}aria2c -x 16 -s 16 https://example.com/largefile.zip axel -n 10 https://example.com/largefile.zip curl --http2 https://example.com curl --compressed https://example.com curl --resolve example.com:443:1.2.3.4 https://example.com
🛡️ 安全增强措施 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 curl --tlsv1.3 https://example.com curl --ciphers ECDHE-RSA-AES128-GCM-SHA256 https://example.com curl --pinnedpubkey "sha256//base64==" https://example.com curl --cert-status https://example.com curl -s -u username:password https://example.com | sed 's/password/***/g' shred -u ~/.curl_history
🤖 自动化脚本示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #!/bin/bash set -euo pipefailBACKUP_URL="https://backup.example.com/upload" API_KEY="your_api_key_here" LOG_FILE="/var/log/backup.log" echo "$(date) : Starting backup" >> "$LOG_FILE " tar -czf /tmp/backup-$(date +%Y%m%d).tar.gz /important/data if curl -X POST -H "Authorization: Bearer $API_KEY " -F "file=@/tmp/backup-$(date +%Y%m%d) .tar.gz" "$BACKUP_URL " ; then echo "$(date) : Backup uploaded successfully" >> "$LOG_FILE " rm -f /tmp/backup-*.tar.gz else echo "$(date) : Backup upload failed" >> "$LOG_FILE " exit 1 fi
⚠️ 注意事项 📜 法律与合规 1 2 3 4 5 6 7 8 9 10 11 wget -e robots=off https://example.com/
🚦 频率限制与礼仪 1 2 3 4 5 6 7 8 9 10 11 12 13 14 wget -w 2 https://example.com/pages/ curl --limit-rate 100k https://example.com curl -H "Cache-Control: max-age=3600" https://example.com wget -T 30 https://example.com
🔒 安全最佳实践 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 echo 'user = "username:password"' > ~/.curlrcchmod 600 ~/.curlrcsudo apt update && sudo apt upgrade wget curlwget https://example.com/file.zip wget https://example.com/file.zip.sha256 sha256sum -c file.zip.sha256
💾 资源管理 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 df -h /download/pathwget -Q100m https://example.com/largefile find /downloads -type f -mtime +30 -delete curl -O https://example.com/largefile.tmp && mv largefile.tmp largefile.final
🐛 错误处理与调试 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 if wget -q https://example.com/file.zip; then echo "Download successful" else echo "Download failed with exit code $?" fi curl -v -o output.txt https://example.com 2> error.log wget --tries=5 --retry-connrefused https://example.com/file.zip curl --connect-timeout 30 --max-time 300 https://example.com curl --interface eth0 https://example.com
📊 总结 🎯 核心要点回顾 wget 优势:
✅ 专为下载优化,简单易用
✅ 支持递归下载和网站镜像
✅ 内置断点续传功能
✅ 适合批量下载任务
✅ 后台运行支持
curl 优势:
✅ 支持 30+ 种协议
✅ 强大的 API 测试能力
✅ 灵活的数据发送功能
✅ 详细的调试信息
✅ 文件上传支持
🔧 学习建议
🎯 初学者 :先从 wget 开始,掌握基本下载功能
🚀 进阶用户 :学习 curl 的复杂请求和数据发送
🏆 高级用户 :掌握两者组合使用和自动化脚本
🛡️ 生产环境 :注重安全性和错误处理
📚 扩展资源 1 2 3 4 5 6 7 8 9 10 11 man wget man curl
🔮 未来发展趋势
🌐 HTTP/3 支持 :curl 已开始支持 HTTP/3
🔐 增强安全 :更好的 TLS 和加密支持
📊 性能优化 :更高效的传输算法
🤖 自动化集成 :更好的脚本和自动化支持
💡 最终建议 :根据具体需求选择合适的工具。对于简单的下载任务,wget 更加方便;对于复杂的网络请求和 API 测试,curl 更为强大。在实际工作中,两者经常结合使用,发挥各自优势。
Linux 下载神器 wget 与 curl 命令详解 📥