Linux常用命令-wget

命令

wget

描述

The non-interactive network downloader
下载工具

用法

1
wget [option]... [URL]...

选项

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
Options:
Logging and input file:
-o, --output-file=FILE 保存标准输出到文件
-a, --append-output=FILE 追加标准输出到文件
-d, --debug 显示debug信息
-q, --quiet 静默模式
-v, --verbose 显示详情,此为默认选项
-nv, --no-verbose 显示精简详情
--report-speed=TYPE Output bandwidth as TYPE. TYPE can be bits.
-i, --input-file=FILE 指定URL文件
-F, --force-html 将输入文件当作HTML文件
-B, --base=URL 解析与URL相关的HTML输入文件,由-i -F选项指定
--config=FILE 指定配置文件

Download:
-b, --background 后台执行
-t, --tries=NUMBER 设置重试次数,0表示无限制
--retry-connrefused 即使连接被拒绝也进行重试
-O, --output-document=FILE 下载文件
-nc, --no-clobber 如果文件名已存在,则跳过下载
-c, --continue 断点续传下载文件
--progress=TYPE 指定进度条类型,TYPE可选参数bar或dot
-N, --timestamping 除非比本地文件更新,否则不下载文件
--no-use-server-timestamps 不要将本地文件的时间戳设置为服务器上的时间戳
-S, --server-response 显示服务器响应信息
--spider 用于测试文件是否存在而不下载任何文件
-T, --timeout=SECONDS 统一设置所有超时时间
--dns-timeout=SECS 设置DNS解析超时时间
--connect-timeout=SECS 设置连接超时时间
--read-timeout=SECS 设置读取超时时间
-w, --wait=SECONDS 设置等待间隔
--waitretry=SECONDS 在获取文件的重试期间等待1..SECONDS秒
--random-wait 获取多个文件时,每次随机等待间隔0.5*WAIT...1.5*WAIT秒
--no-proxy 禁止使用代理
-Q, --quota=NUMBER 设置检索配额
--bind-address=ADDRESS 绑定到被第主机上的地址(主机名或IP)
--limit-rate=RATE 限制下载速率
--no-dns-cache 关闭DNS缓存
--restrict-file-names=OS 限定文件名中的字符为OS允许的字符
--ignore-case 匹配文件或目录时忽略大小写
-4, --inet4-only 只使用ipv4地址
-6, --inet6-only 只使用ipv6地址
--prefer-family=FAMILY 首先使用指定协议的地址,FAMILY可选参数为IPv6, IPv4, or none
--user=USER 设置ftp和http的用户名
--password=PASS 设置ftp和http的用户密码
--ask-password 提示输入密码
--no-iri 关闭IRI支持
--local-encoding=ENC 使用ENC作为IRIs的本地编码
--remote-encoding=ENC 使用ENC作为默认远程编码
--unlink remove file before clobber.

Directories:
-nd, --no-directories 不要创建目录
-x, --force-directories 强制创建目录
-nH, --no-host-directories 不要创建主机目录
--protocol-directories 在目录中使用协议名
-P, --directory-prefix=PREFIX 指定目录前缀 PREFIX/...
--cut-dirs=NUMBER 忽略远程目录中多少层目录层级

HTTP options:
--http-user=USER 设置http用户名
--http-password=PASS 设置http用户密码
--no-cache 禁止服务器缓存数据
--default-page=NAME 更改默认页面名称,默认为index.html
-E, --adjust-extension 使用适当的扩展名保存HTML/CSS文件
--ignore-length 忽略'Content-Length'头部字段
--header=STRING 增加header信息
--max-redirect 每页允许的最大重定向次数
--proxy-user=USER 设置代理用户名
--proxy-password=PASS 设置代理用户密码
--referer=URL 设置Referer信息
--save-headers 追加保存header信息到下载的文件头部
-U, --user-agent=AGENT 设置UA
--no-http-keep-alive 关闭http keep-alive
--no-cookies 不使用cookies
--load-cookies=FILE 从文件加载cookie
--save-cookies=FILE 保存cookie到文件
--keep-session-cookies 加载并保存会话cookies
--post-data=STRING 使用POST方法发送信息
--post-file=FILE 使用POST方法发送文件
--content-disposition honor the Content-Disposition header when
choosing local file names (EXPERIMENTAL).
--content-on-error output the received content on server errors.
--auth-no-challenge 在不等待服务器询问的情况下发送基本的HTTP身份验证信息

HTTPS (SSL/TLS) options:
--secure-protocol=PR 选择加密协议,可选参数为auto, SSLv2, SSLv3, TLSv1, TLSv1_1 and TLSv1_2
--no-check-certificate 不验证服务器的证书
--certificate=FILE 指定客户端证书文件
--certificate-type=TYPE 指定客户端证书类型,可选PEM or DER
--private-key=FILE 指定私钥文件
--private-key-type=TYPE 指定私钥文件类型,可选PEM or DER.
--ca-certificate=FILE 指定CA文件
--ca-directory=DIR 指定CA目录
--random-file=FILE file with random data for seeding the SSL PRNG.
--egd-file=FILE file naming the EGD socket with random data.

FTP options:
--ftp-user=USER set ftp user to USER.
--ftp-password=PASS set ftp password to PASS.
--no-remove-listing don't remove `.listing' files.
--no-glob turn off FTP file name globbing.
--no-passive-ftp disable the "passive" transfer mode.
--preserve-permissions preserve remote file permissions.
--retr-symlinks when recursing, get linked-to files (not dir).

WARC options:
--warc-file=FILENAME save request/response data to a .warc.gz file.
--warc-header=STRING insert STRING into the warcinfo record.
--warc-max-size=NUMBER set maximum size of WARC files to NUMBER.
--warc-cdx write CDX index files.
--warc-dedup=FILENAME do not store records listed in this CDX file.
--no-warc-compression do not compress WARC files with GZIP.
--no-warc-digests do not calculate SHA1 digests.
--no-warc-keep-log do not store the log file in a WARC record.
--warc-tempdir=DIRECTORY location for temporary files created by the
WARC writer.

Recursive download:
-r, --recursive 递归下载,可用于下载整个页面资源
-l, --level=NUMBER 最大递归层级,inf或0表示无限制
--delete-after 下载完成后删除本地文件
-k, --convert-links 下载的HTML或CSS中的链接文件并转换为本地链接
--backups=N 最多保存几个文件副本
-K, --backup-converted 覆盖文件前先备份文件为X.orig #经测试该选项无效!
-m, --mirror 等同于-N -r -l inf --no-remove-listing
-p, --page-requisites get all images, etc. needed to display HTML page.
--strict-comments turn on strict (SGML) handling of HTML comments.

Recursive accept/reject:
-A, --accept=LIST 接受的扩展名列表,用逗号分隔
-R, --reject=LIST 拒绝的扩展名列表,用逗号分隔
--accept-regex=REGEX 正则匹配接受的URL
--reject-regex=REGEX 正则匹配拒绝的URL
--regex-type=TYPE 正则类型 (posix|pcre)
-D, --domains=LIST 接受的域名列表,用逗号分隔
--exclude-domains=LIST 拒绝的域名列表,用逗号分隔
--follow-ftp 跟随HTML文件中的FTP链接
--follow-tags=LIST 跟随HTML标记列表,用逗号分隔
--ignore-tags=LIST 忽略HTML标记列表,用逗号分隔
-H, --span-hosts 递归时转到外部主机
-L, --relative 只跟随相对链接
-I, --include-directories=LIST 允许的目录列表
--trust-server-names use the name specified by the redirection url last component.
-X, --exclude-directories=LIST 排除的目录列表
-np, --no-parent 不追溯父目录

注意

参考 https://www.gnu.org/software/wget/manual/wget.html

示例

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
$ wget  -V
GNU Wget 1.14 built on linux-gnu.
+digest +https +ipv6 +iri +large-file +nls +ntlm +opie +ssl/openssl

# 下载文件,多次下载默认会增加.x后缀
$ wget https://www.a.com/index.html
$ wget https://www.a.com/index.html
$ ll index.html*
-rw-r--r-- 1 root root 2443 Oct 24 21:19 index.html
-rw-r--r-- 1 root root 2443 Oct 24 21:20 index.html.1
# -O 下载文件,并指定文件名,多次下载默认会自动覆盖
$ wget -O a.html https://www.a.com/index.html
# -o 保存标准输出到文件
$ wget -o wget.log -O a.html https://www.a.com/index.html
# -q 静默模式,不显示任何输出
$ wget -q -O a.html https://www.a.com/index.html
# -nv 显示精简输出
$ wget -nv -O a.html https://www.a.com/index.html
# -i 指定URL文件,用于下载多个URL
$ echo https://www.a.com/index1.html >> file
$ echo https://www.a.com/index2.html >> file
$ wget -i file
# -b 后台执行,如下载较大文件可放到后台执行,默认会生成一个wget-log的日志文件
$ wget -b https://www.a.com/bigfile.tar.gz
# -nc 如果文件已存在,则跳过下载
$ wget -nc https://www.a.com/index.html
File ‘index.html’ already there; not retrieving.
# -c 断点续传,可以配合-b使用
$ wget -bc https://www.a.com/bigfile.tar.gz
# -N 远程文件比本地文件更新则下载,否则跳过
$ wget -N https://www.a.com/index.html
# --spider 用于测试远程文件是否存在,即URL是否正确
$ wget --spider https://www.a.com/index.html
Spider mode enabled. Check if remote file exists.
--2021-10-24 21:41:37-- https://www.a.com/index.html
...
HTTP request sent, awaiting response... 200 OK
Length: 2443 (2.4K) [text/html]
Remote file exists and could contain further links,
but recursion is disabled -- not retrieving.
# -T 统一设置超时时间
$ wget -T 3 https://www.a.com/index.html
# --limit-rate 限制下载速率
$ wget --limit-rate=300k https://www.a.com/bigfile.tar.gz
# --ignore-case 忽略文件或目录名的大小写
$ wget --ignore-case https://www.a.com/Index.html
# -P 指定目录前缀,可用于批量下载时指定单独的目录
$ wget -P a -i file
# -r 递归下载, -p 获取html文件中的images资源, -k 获取html中的链接, -np 不追溯父目录
# 如下命令可下载该页面及所有相关依赖文件
$ wget -r -p -np -k https://www.a.com/index.html
# -A 只下载指定后缀的文件
$ wget -r -p -np -k -R jpg,png https://www.a.com/index.html
# -R 过滤指定后缀的文件,跳过
$ wget -r -p -np -k -R gif,jpg,png,svg https://www.a.com/index.html