Linux 下载神器 wget 与 curl 命令详解 📥

🌐 掌握Linux下最强大的网络传输工具,轻松应对各种下载和数据传输需求


✨ 工具特点

  • 高效传输: 支持多协议、高速下载和上传
  • 🔄 断点续传: 网络中断后可从断点继续传输
  • 🛡️ 安全可靠: 支持 SSL/TLS 加密和多种认证方式
  • 📊 灵活可控: 丰富的参数选项满足各种需求
  • 🐧 跨平台: 在几乎所有 Linux 发行版和 Unix 系统上可用

📖 目录导航


🔧 安装方法

🐧 Ubuntu/Debian 系统

1
2
3
4
5
6
7
8
9
10
11
12
# 更新软件包列表
sudo apt update

# 安装 wget
sudo apt install wget -y

# 安装 curl
sudo apt install curl -y

# 验证安装
wget --version
curl --version

🔴 CentOS/RHEL 系统

1
2
3
4
5
# 安装 wget 和 curl
sudo yum install wget curl -y

# 或者使用 dnf(CentOS 8+)
sudo dnf install wget curl -y

🍎 macOS 系统

1
2
3
4
5
# 使用 Homebrew 安装
brew install wget curl

# 或者使用 MacPorts
sudo port install wget curl

📦 源码编译安装(高级用户)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 编译安装 wget
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 install

# 编译安装 curl
wget 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-log

# 设置超时时间(秒)
wget -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
# 限制下载速度(100KB/s)
wget --limit-rate=100k https://example.com/large_file.iso

# 限制下载速度(1MB/s)
wget --limit-rate=1m https://example.com/large_file.iso

# 禁用缓存
wget --no-cache https://example.com/file.html

# 使用被动模式 FTP
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
# 递归下载整个网站(最多5层)
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
# FTP 认证下载
wget --ftp-user=username --ftp-password=password ftp://example.com/file.zip

# HTTP 基础认证
wget --user=username --password=password https://example.com/protected/file.zip

# 忽略 SSL 证书验证(不安全)
wget --no-check-certificate https://example.com/secure-file.zip

# 使用特定 SSL 版本
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
# 从文件读取URL列表下载
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/

# 强制使用 IPv4 或 IPv6
wget -4 https://example.com # IPv4
wget -6 https://example.com # IPv6

📡 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
# GET 请求(默认)
curl https://api.example.com/users

# POST 请求
curl -X POST https://api.example.com/users
curl -X POST -d "name=John&age=30" https://api.example.com/users

# PUT 请求
curl -X PUT -d "name=John" https://api.example.com/users/1

# DELETE 请求
curl -X DELETE https://api.example.com/users/1

# PATCH 请求
curl -X PATCH -d "age=31" https://api.example.com/users/1

# HEAD 请求
curl -I https://example.com

# OPTIONS 请求
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&param2=value2" https://example.com/form

# 发送 JSON 数据
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

# URL 编码数据
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

# Bearer Token 认证
curl -H "Authorization: Bearer your_token" https://api.example.com/data

# Digest 认证
curl --digest -u username:password https://example.com/protected

# NTLM 认证
curl --ntlm -u username:password https://example.com/protected

# 忽略 SSL 证书验证
curl -k https://example.com/secure

# 使用特定客户端证书
curl --cert client.pem --key key.pem https://example.com/secure

# 使用 CA 证书包
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 7.66.0+)
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

# 使用 SOCKS 代理
curl --socks5 proxy:port https://example.com

# 使用 HTTP 代理
curl -x http://proxy:port https://example.com

# 保存 cookie
curl -c cookies.txt https://example.com/login

# 使用 cookie
curl -b cookies.txt https://example.com/dashboard

# 上传文件
curl -T localfile.txt https://example.com/upload

# 上传多个文件
curl -T "{file1,file2}" https://example.com/upload

# FTP 操作
curl -u user:pass ftp://example.com/ -O
curl -Q "CWD /remote/directory" ftp://example.com/

# 邮件传输(SMTP)
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
# 1. 下载大型文件(支持断点续传)
wget -c https://example.com/large_file.iso

# 2. 镜像整个网站用于离线浏览
wget -mk -w 2 -np https://example.com/

# 3. 批量下载文件列表
wget -i url_list.txt -P /path/to/downloads

# 4. 定时自动下载(结合cron)
# 每天凌晨下载更新
0 0 * * * wget -q -O /backups/update.zip https://example.com/daily_update.zip

# 5. 下载特定类型文件
wget -r -A "*.pdf" https://example.com/documents/

# 6. 受限网络环境下载
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
# 1. REST API 测试和调试
curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer token" -d '{"user":"test"}' https://api.example.com/users

# 2. 文件上传到服务器
curl -F "file=@localfile.txt" -F "description=test file" https://example.com/upload

# 3. 网站健康状态监控
curl -s -o /dev/null -w "%{http_code}\n" https://example.com

# 4. 获取天气信息
curl "wttr.in/Beijing?format=3"

# 5. 测试接口性能
curl -w "DNS: %{time_namelookup} Connect: %{time_connect} Start: %{time_starttransfer} Total: %{time_total}\n" https://api.example.com

# 6. OAuth 2.0 认证流程
curl -u client_id:client_secret -d "grant_type=client_credentials" https://oauth.example.com/token

# 7. WebSocket 连接测试
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
# 1. 下载并解压文件
curl -L https://example.com/archive.tar.gz | tar xz

# 2. 安全的脚本下载和执行
curl -sSL https://example.com/install.sh | bash -s -- --option value

# 3. 监控多个网站状态
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

# 4. 批量处理 API 请求
cat requests.txt | while read url; do
curl -X POST -H "Content-Type: application/json" -d @data.json "$url"
done

# 5. 下载检查文件完整性
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
# 彩色输出(需要安装 lolcat)
curl -s https://example.com/text | lolcat

# JSON 格式化输出
curl -s https://api.example.com/data | jq .

# XML 格式化输出
curl -s https://api.example.com/data | xmllint --format -

# CSV 处理
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
# 使用 HTTP 代理
curl -x http://proxy:8080 https://example.com

# 使用 SOCKS 代理
curl --socks5 proxy:1080 https://example.com

# 代理认证
curl -x http://user:pass@proxy:8080 https://example.com

# 通过 SSH 隧道
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
# 并行下载(使用 xargs)
cat url_list.txt | xargs -P 4 -I {} wget {}

# 多连接下载(使用 aria2c)
aria2c -x 16 -s 16 https://example.com/largefile.zip

# 多连接下载(使用 axel)
axel -n 10 https://example.com/largefile.zip

# 连接复用(HTTP/2)
curl --http2 https://example.com

# 压缩传输
curl --compressed https://example.com

# DNS 预解析
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
# 使用 TLS 1.3
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 pipefail

BACKUP_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
# 1. 尊重 robots.txt 协议
wget -e robots=off https://example.com/ # 谨慎使用

# 2. 遵守版权法规
# 仅下载拥有合法权限的内容

# 3. 注意使用条款
# 查看网站的使用条款和条件

# 4. 数据保护法规
# 遵守 GDPR、CCPA 等数据保护法规

🚦 频率限制与礼仪

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 1. 添加延迟避免过度请求
wget -w 2 https://example.com/pages/

# 2. 限制并发连接
curl --limit-rate 100k https://example.com

# 3. 使用缓存减少请求
curl -H "Cache-Control: max-age=3600" https://example.com

# 4. 设置合理的超时时间
wget -T 30 https://example.com

# 5. 监控服务器负载
# 如果服务器响应变慢,减少请求频率

🔒 安全最佳实践

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 1. 避免在命令行中暴露密码
# 错误:curl -u user:password https://example.com
# 正确:curl -u user https://example.com # 会提示输入密码

# 2. 使用配置文件存储敏感信息
echo 'user = "username:password"' > ~/.curlrc
chmod 600 ~/.curlrc

# 3. 定期更新软件
sudo apt update && sudo apt upgrade wget curl

# 4. 验证下载文件的完整性
wget https://example.com/file.zip
wget https://example.com/file.zip.sha256
sha256sum -c file.zip.sha256

# 5. 使用 HTTPS 加密连接
# 优先使用 https:// 而不是 http://

💾 资源管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 1. 监控磁盘空间
# 大文件下载前检查可用空间
df -h /download/path

# 2. 限制下载大小
wget -Q100m https://example.com/largefile

# 3. 清理旧文件
find /downloads -type f -mtime +30 -delete

# 4. 使用临时文件处理大文件
curl -O https://example.com/largefile.tmp && mv largefile.tmp largefile.final

# 5. 内存使用优化
# 对于大文件,使用流式处理而不是全部加载到内存

🐛 错误处理与调试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 1. 检查退出状态码
if wget -q https://example.com/file.zip; then
echo "Download successful"
else
echo "Download failed with exit code $?"
fi

# 2. 详细的错误日志
curl -v -o output.txt https://example.com 2> error.log

# 3. 重试机制
wget --tries=5 --retry-connrefused https://example.com/file.zip

# 4. 超时处理
curl --connect-timeout 30 --max-time 300 https://example.com

# 5. 网络诊断
curl --interface eth0 https://example.com # 指定网络接口

📊 总结

🎯 核心要点回顾

wget 优势:

  • ✅ 专为下载优化,简单易用
  • ✅ 支持递归下载和网站镜像
  • ✅ 内置断点续传功能
  • ✅ 适合批量下载任务
  • ✅ 后台运行支持

curl 优势:

  • ✅ 支持 30+ 种协议
  • ✅ 强大的 API 测试能力
  • ✅ 灵活的数据发送功能
  • ✅ 详细的调试信息
  • ✅ 文件上传支持

🔧 学习建议

  1. 🎯 初学者:先从 wget 开始,掌握基本下载功能
  2. 🚀 进阶用户:学习 curl 的复杂请求和数据发送
  3. 🏆 高级用户:掌握两者组合使用和自动化脚本
  4. 🛡️ 生产环境:注重安全性和错误处理

📚 扩展资源

1
2
3
4
5
6
7
8
9
10
11
# 查看完整文档
man wget
man curl

# 在线文档
# wget: https://www.gnu.org/software/wget/manual/
# curl: https://curl.se/docs/

# 社区支持
# GitHub 仓库和问题跟踪
# Stack Overflow 相关问题

🔮 未来发展趋势

  1. 🌐 HTTP/3 支持:curl 已开始支持 HTTP/3
  2. 🔐 增强安全:更好的 TLS 和加密支持
  3. 📊 性能优化:更高效的传输算法
  4. 🤖 自动化集成:更好的脚本和自动化支持

💡 最终建议:根据具体需求选择合适的工具。对于简单的下载任务,wget 更加方便;对于复杂的网络请求和 API 测试,curl 更为强大。在实际工作中,两者经常结合使用,发挥各自优势。