Jacky's blog
首页
  • 学习笔记

    • web
    • android
    • iOS
    • vue
  • 分类
  • 标签
  • 归档
收藏
  • tool
  • algo
  • python
  • java
  • server
  • growth
  • frida
  • blog
  • SP
  • more
GitHub (opens new window)

Jack Yang

编程; 随笔
首页
  • 学习笔记

    • web
    • android
    • iOS
    • vue
  • 分类
  • 标签
  • 归档
收藏
  • tool
  • algo
  • python
  • java
  • server
  • growth
  • frida
  • blog
  • SP
  • more
GitHub (opens new window)
  • shell

    • shell 入门指南
    • linux 入门指南
    • Shell 常用命令速查手册
    • Shell 代码片段集合
    • brew
    • nvim
    • awk
    • sed
    • xargs
    • fzf
    • fd
    • ssh
    • ftp
    • sftp
    • ifconfig
    • wget
      • wget vs curl
      • base
      • common options
      • case
      • 注意事项
    • watch
  • tool

  • client

  • 网络

  • compute_base

  • blog

  • growth

  • java

  • C&C++

  • ai

  • secure

  • cms

  • english

  • 生活

  • 金融学

  • more

  • other
  • shell
Jacky
2026-09-09
目录

wget

wget 是 GNU 项目的非交互式网络下载工具,专为"下载文件"设计。支持断点续传、递归下载、后台下载、整站镜像,适合脚本和批量下载场景。

# wget vs curl

维度 wget curl
定位 下载工具 数据传输工具
递归下载 ✅ 原生支持 ❌ 不支持
断点续传 ✅ -c ✅ -C -
后台下载 ✅ -b ❌ 需配合 nohup
整站镜像 ✅ --mirror ❌ 不支持
上传文件 ❌ 不支持 ✅ -T / -F
API 调用 一般 ✅ 强大(-H、-d、-X 等)
协议支持 HTTP/HTTPS/FTP 几乎所有协议

简单记忆:下载文件/整站用 wget,调用接口/上传用 curl。

# base

wget [选项] URL
1

默认下载到当前目录,文件名取 URL 最后一段。

# common options

基本控制

  • -O, --output-document=<文件>:指定输出文件名(-O - 输出到 stdout)
  • -P, --directory-prefix=<目录>:指定保存目录
  • -c, --continue:断点续传(继续未完成的下载)
  • -q, --quiet:安静模式,不输出进度
  • -v, --verbose:详细模式
  • -b, --background:后台下载,日志写入 wget-log
  • -i, --input-file=<文件>:从文件读取 URL 列表批量下载
  • --limit-rate=<速度>:限制下载速度(如 1M、500k)
  • --spider:蜘蛛模式,只检查链接有效性不下载

HTTP 相关

  • --user=<用户名> --password=<密码>:HTTP 基本认证
  • --user-agent=<UA>:指定 User-Agent
  • --header=<头部>:添加自定义请求头(可多次使用)
  • --no-check-certificate:不验证 SSL 证书(自签名证书时用)
  • -S, --server-response:显示服务器响应头
  • --post-data=<数据> / --post-file=<文件>:POST 请求

递归下载

  • -r, --recursive:递归下载
  • -l, --level=<深度>:递归深度(默认 5,inf 为无限)
  • -np, --no-parent:不递归到父目录(强烈建议加上,避免下载整个网站)
  • -A, --accept=<列表>:只下载匹配的文件类型(如 pdf,html)
  • -R, --reject=<列表>:排除匹配的文件类型
  • -I, --include-directories=<列表>:只递归指定目录
  • -X, --exclude-directories=<列表>:排除指定目录
  • --mirror:镜像模式(等价于 -r -N -l inf --no-remove-listing)
  • -k, --convert-links:下载完成后将链接转换为本地相对路径
  • -p, --page-requisites:下载页面所需的所有资源(图片、CSS、JS 等)

# case

基础下载

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

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

# 指定保存目录
wget -P ~/Downloads https://example.com/file.zip
1
2
3
4
5
6
7
8

断点续传与大文件

# 断点续传(下载中断后重新执行同一命令即可继续)
wget -c https://example.com/large-file.iso

# 后台下载(日志在 wget-log,可用 tail -f 查看)
wget -b https://example.com/large-file.iso

# 限制下载速度为 1MB/s
wget --limit-rate=1M https://example.com/file.zip
1
2
3
4
5
6
7
8

批量下载

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

# 批量下载并指定文件名前缀
wget -i urls.txt -P ./downloads
1
2
3
4
5

递归下载与整站镜像

# 递归下载某个目录下的所有 PDF(不进入父目录)
wget -r -np -A pdf https://example.com/docs/

# 递归深度为 3,只下载 html 和 pdf
wget -r -l 3 -np -A html,pdf https://example.com/blog/

# 镜像整个网站(转换链接为本地路径,下载所有页面资源)
wget --mirror -k -p https://example.com

# 镜像网站但排除某些目录
wget --mirror -X /admin,/private https://example.com
1
2
3
4
5
6
7
8
9
10
11

链接检查与调试

# 检查 URL 是否可访问(不下载)
wget --spider https://example.com/file.zip

# 批量检查 urls.txt 中的链接有效性
wget --spider -i urls.txt

# 显示服务器响应头(调试用)
wget -S --spider https://example.com
1
2
3
4
5
6
7
8

特殊场景

# 下载需要登录的页面(Cookie)
wget --cookies=on --load-cookies=cookies.txt https://example.com/protected

# 绕过 SSL 证书验证(自签名证书)
wget --no-check-certificate https://self-signed.example.com

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

# 输出到 stdout 并管道处理
wget -O - https://example.com/data.json | jq .
1
2
3
4
5
6
7
8
9
10
11

# 注意事项

  • 递归下载务必加 -np(no-parent),否则可能从一个页面爬到整个网站
  • --mirror 模式会保留时间戳,适合做网站备份;普通下载用 -r 即可
  • 断点续传 -c 要求服务器支持 Range 请求,部分服务器不支持
  • 后台下载 -b 后,日志文件默认是当前目录的 wget-log,多个后台任务会递增编号
  • macOS 可通过 brew install wget 安装
#Shell#tool#Linux
上次更新: 2026/09/10, 14:32:04
ifconfig
watch

← ifconfig watch→

最近更新
01
direnv
09-09
02
nvim
09-09
03
watch
09-09
更多文章>
Theme by Vdoing | Copyright © 2019-2026 Jacky | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式