Administrator
发布于 2026-05-17 / 12 阅读
0
0

wget 命令完全使用指南

wget 命令完全使用指南

wget 是 GNU 项目下的命令行下载工具,支持 HTTP/HTTPS/FTP 协议,以递归下载和断点续传能力著称,是 Linux 系统管理员和开发者的必备工具。


📦 安装 wget

# Debian/Ubuntu
sudo apt install wget

# CentOS/RHEL
sudo yum install wget

# macOS
brew install wget

# 验证安装
wget --version

🚀 基础用法

简单下载

# 下载文件
wget https://example.com/file.zip

# 指定保存文件名
wget -O download.zip https://example.com/file.zip

# 保存到指定目录
wget -P /tmp/ https://example.com/file.zip

下载并显示信息

# 显示详细信息
wget -v https://example.com/file.zip

# 静默模式(不输出)
wget -q https://example.com/file.zip

# 显示进度条
wget --progress=bar:force https://example.com/file.zip

⏯️ 断点续传

# 继续未完成的下载
wget -c https://example.com/large-file.iso

# 强制断点续传(即使服务器不支持)
wget -c --tries=10 https://example.com/file.zip

🔄 递归下载

下载整个网站

# 镜像整个网站
wget -m https://example.com

# 限制递归深度
wget -r -l 2 https://example.com

# 下载指定类型文件
wget -r -A "*.html" https://example.com

配合拒绝列表

# 排除特定目录
wget -r --reject="/admin/*" https://example.com

# 排除文件类型
wget -r --reject="*.gif,*.jpg" https://example.com

# 使用拒绝列表文件
wget -r --reject-file=reject.txt https://example.com

⚙️ 常用参数

请求控制

# 设置 User-Agent
wget --user-agent="Mozilla/5.0" https://example.com

# 设置请求头
wget --header="Authorization: Bearer TOKEN" https://api.example.com/data

# POST 请求
wget --post-data="user=admin&pass=123" https://example.com/login

# POST JSON
wget --post-file=data.json --header="Content-Type: application/json" https://api.example.com/data

下载控制

# 限速下载(1MB/s)
wget --limit-rate=1m https://example.com/file.zip

# 设置重试次数
wget --tries=10 https://example.com/file.zip

# 等待重试(秒)
wget --wait=5 --tries=10 https://example.com/file.zip

# 超时设置
wget --timeout=30 https://example.com/file.zip

输出控制

# 覆盖已有文件
wget -O file.zip --no-clobber https://example.com/file.zip

# 后台下载
wget -b https://example.com/large-file.iso

# 查看后台下载日志
tail -f wget-log

🔐 认证方式

HTTP Basic 认证

wget --user=username --password=password https://example.com/protected/file

# 或在 URL 中(不推荐)
wget https://username:password@example.com/file

使用 .netrc 文件

创建 ~/.netrc

machine example.com
login username
password password
chmod 600 ~/.netrc
wget --no-netrc https://example.com/protected/file

🌐 代理设置

# HTTP/HTTPS 代理
wget -e use_proxy=yes -e http_proxy=http://proxy:8080 https://example.com

# SOCKS 代理
wget --execute="https_proxy = http://localhost:1080" https://example.com

# 使用环境变量
export http_proxy=http://proxy:8080
export https_proxy=http://proxy:8080
wget https://example.com

🛠️ 实战场景

1. 批量下载

# 从文件读取 URL 列表下载
wget -i urls.txt

# urls.txt 内容:
# https://example.com/file1.zip
# https://example.com/file2.zip
# https://example.com/file3.zip

2. 下载并重命名

# 单个文件重命名
wget -O newname.zip https://example.com/oldname.zip

# 批量下载并保持原始文件名
wget --content-disposition https://example.com/files/

3. 定时下载

# 配合 cron 定时下载
# crontab -e
0 2 * * * /usr/bin/wget -q -O /tmp/backup.tar.gz https://example.com/backup.tar.gz

4. 镜像网站

# 完整镜像(保持链接结构)
wget -m -p -E -k https://example.com

# 参数说明:
# -m: 镜像模式
# -p: 下载页面所需资源(图片、CSS)
# -E: 将 HTML 文件保存为 .html
# -k: 转换链接为本地链接

5. 下载 FTP 资源

# 匿名 FTP
wget ftp://ftp.example.com/pub/file.zip

# 带认证的 FTP
wget --ftp-user=username --ftp-password=password ftp://ftp.example.com/file.zip

# 递归下载 FTP 目录
wget -r ftp://ftp.example.com/pub/

6. 下载 GitHub Release

# 下载最新 release
wget https://github.com/user/repo/releases/latest/download/app-linux-amd64

# 下载指定版本
wget https://github.com/user/repo/releases/download/v1.0.0/app-linux-amd64

📋 高级技巧

使用配置文件

创建 ~/.wgetrc

# 默认限速
limit_rate = 1m

# 默认重试次数
tries = 5

# 默认等待时间
wait = 3

# 不覆盖已有文件
no_clobber = on

组合命令

# 下载并解压
wget -qO- https://example.com/archive.tar.gz | tar xz

# 下载并执行
wget -qO- https://example.com/install.sh | bash

# 下载并校验
wget https://example.com/file.zip && md5sum file.zip

错误处理

# 下载失败时重试
wget --tries=10 --retry-connrefused https://example.com/file.zip

# 忽略 SSL 错误(慎用)
wget --no-check-certificate https://self-signed.example.com/file

# 仅下载比本地新的文件
wget -N https://example.com/file.zip

🆚 wget vs curl

特性wgetcurl
主要用途文件下载API 测试、数据传输
输出默认保存文件默认输出到 stdout
递归下载✅ 强大支持❌ 不支持
断点续传-c-C -
POST 支持⚠️ 基础支持✅ 完整支持
后台下载-b❌ 需配合其他工具
复杂请求⚠️ 有限✅ 完整支持

📚 速查表

# 下载文件
wget URL

# 指定文件名
wget -O filename URL

# 指定目录
wget -P /path/ URL

# 断点续传
wget -c URL

# 后台下载
wget -b URL

# 限速下载
wget --limit-rate=1m URL

# 递归下载
wget -r -l depth URL

# 镜像网站
wget -m URL

# 批量下载
wget -i urls.txt

# 静默模式
wget -q URL

# 设置重试
wget --tries=N URL

# HTTP 认证
wget --user=u --password=p URL

💡 小贴士

  1. 大文件用 -c:网络不稳时务必使用断点续传
  2. 限速下载:避免带宽被打满影响其他服务
  3. 后台下载:大文件用 -b 后台执行,查看 wget-log 进度
  4. 配合 cron:定时下载备份文件
  5. 递归下载要谨慎:设置合理的深度和范围,避免下载过多

wget 是下载任务的最佳伙伴,curl 是 API 交互的利器——两者各有所长,配合使用更强大。 🤝


评论