【Linux】一步一步学Linux——wget命令(192)
发布日期:2021-06-29 20:44:54 浏览次数:2 分类:技术文章

本文共 13444 字,大约阅读时间需要 44 分钟。

00. 目录

文章目录

01. 命令概述

wget命令用来从指定的URL下载文件。wget非常稳定,它在带宽很窄的情况下和不稳定网络中有很强的适应性,如果是由于网络的原因下载失败,wget会不断的尝试,直到整个文件下载完毕。如果是服务器打断下载过程,它会再次联到服务器上从停止的地方继续下载。这对从那些限定了链接时间的服务器上下载大文件非常有用。

wget支持HTTP,HTTPS和FTP协议,可以使用HTTP代理。所谓的自动下载是指,wget可以在用户退出系统的之后在后台执行。这意味这你可以登录系统,启动一个wget下载任务,然后退出系统,wget将在后台执行直到任务完成,相对于其它大部分浏览器在下载大量数据时需要用户一直的参与,这省去了极大的麻烦。

02. 命令格式

用法: wget [选项]... [URL]...

03. 常用选项

长选项所必须的参数在使用短选项时也是必须的。启动:  -V,  --version           显示 Wget 的版本信息并退出。  -h,  --help              打印此帮助。  -b,  --background        启动后转入后台。  -e,  --execute=COMMAND   运行一个“.wgetrc”风格的命令。日志和输入文件:  -o,  --output-file=FILE    将日志信息写入 FILE。  -a,  --append-output=FILE  将信息添加至 FILE。  -d,  --debug               打印大量调试信息。  -q,  --quiet               安静模式 (无信息输出)。  -v,  --verbose             详尽的输出 (此为默认值)。  -nv, --no-verbose          关闭详尽输出,但不进入安静模式。  -i,  --input-file=FILE     下载本地或外部 FILE 中的 URLs。  -F,  --force-html          把输入文件当成 HTML 文件。  -B,  --base=URL            解析与 URL 相关的                             HTML 输入文件 (由 -i -F 选项指定)。       --config=FILE         Specify config file to use.下载:  -t,  --tries=NUMBER            设置重试次数为 NUMBER (0 代表无限制)。       --retry-connrefused       即使拒绝连接也是重试。  -O,  --output-document=FILE    将文档写入 FILE。  -nc, --no-clobber              skip downloads that would download to                                 existing files (overwriting them).  -c,  --continue                断点续传下载文件。       --progress=TYPE           选择进度条类型。  -N,  --timestamping            只获取比本地文件新的文件。  --no-use-server-timestamps     不用服务器上的时间戳来设置本地文件。  -S,  --server-response         打印服务器响应。       --spider                  不下载任何文件。  -T,  --timeout=SECONDS         将所有超时设为 SECONDS 秒。       --dns-timeout=SECS        设置 DNS 查寻超时为 SECS 秒。       --connect-timeout=SECS    设置连接超时为 SECS 秒。       --read-timeout=SECS       设置读取超时为 SECS 秒。  -w,  --wait=SECONDS            等待间隔为 SECONDS 秒。       --waitretry=SECONDS       在获取文件的重试期间等待 1..SECONDS 秒。       --random-wait             获取多个文件时,每次随机等待间隔                                 0.5*WAIT...1.5*WAIT 秒。       --no-proxy                禁止使用代理。  -Q,  --quota=NUMBER            设置获取配额为 NUMBER 字节。       --bind-address=ADDRESS    绑定至本地主机上的 ADDRESS (主机名或是 IP)。       --limit-rate=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 或是 none。       --user=USER               将 ftp 和 http 的用户名均设置为 USER。       --password=PASS           将 ftp 和 http 的密码均设置为 PASS。       --ask-password            提示输入密码。       --no-iri                  关闭 IRI 支持。       --local-encoding=ENC      IRI (国际化资源标识符) 使用 ENC 作为本地编码。       --remote-encoding=ENC     使用 ENC 作为默认远程编码。       --unlink                  remove file before clobber.目录:  -nd, --no-directories           不创建目录。  -x,  --force-directories        强制创建目录。  -nH, --no-host-directories      不要创建主目录。       --protocol-directories     在目录中使用协议名称。  -P,  --directory-prefix=PREFIX  以 PREFIX/... 保存文件       --cut-dirs=NUMBER          忽略远程目录中 NUMBER 个目录层。HTTP 选项:       --http-user=USER        设置 http 用户名为 USER。       --http-password=PASS    设置 http 密码为 PASS。       --no-cache              不在服务器上缓存数据。       --default-page=NAME     改变默认页                               (默认页通常是“index.html”)。  -E,  --adjust-extension      以合适的扩展名保存 HTML/CSS 文档。       --ignore-length         忽略头部的‘Content-Length’区域。       --header=STRING         在头部插入 STRING。       --max-redirect          每页所允许的最大重定向。       --proxy-user=USER       使用 USER 作为代理用户名。       --proxy-password=PASS   使用 PASS 作为代理密码。       --referer=URL           在 HTTP 请求头包含‘Referer: URL’。       --save-headers          将 HTTP 头保存至文件。  -U,  --user-agent=AGENT      标识为 AGENT 而不是 Wget/VERSION。       --no-http-keep-alive    禁用 HTTP keep-alive (永久连接)。       --no-cookies            不使用 cookies。       --load-cookies=FILE     会话开始前从 FILE 中载入 cookies。       --save-cookies=FILE     会话结束后保存 cookies 至 FILE。       --keep-session-cookies  载入并保存会话 (非永久) cookies。       --post-data=STRING      使用 POST 方式;把 STRING 作为数据发送。       --post-file=FILE        使用 POST 方式;发送 FILE 内容。       --content-disposition   当选中本地文件名时                               允许 Content-Disposition 头部 (尚在实验)。       --auth-no-challenge     发送不含服务器询问的首次等待                               的基本 HTTP 验证信息。HTTPS (SSL/TLS) 选项:       --secure-protocol=PR     选择安全协议,可以是 auto、SSLv2、                                SSLv3 或是 TLSv1 中的一个。       --no-check-certificate   不要验证服务器的证书。       --certificate=FILE       客户端证书文件。       --certificate-type=TYPE  客户端证书类型,PEM 或 DER。       --private-key=FILE       私钥文件。       --private-key-type=TYPE  私钥文件类型,PEM 或 DER。       --ca-certificate=FILE    带有一组 CA 认证的文件。       --ca-directory=DIR       保存 CA 认证的哈希列表的目录。       --random-file=FILE       带有生成 SSL PRNG 的随机数据的文件。       --egd-file=FILE          用于命名带有随机数据的 EGD 套接字的文件。FTP 选项:       --ftp-user=USER         设置 ftp 用户名为 USER。       --ftp-password=PASS     设置 ftp 密码为 PASS。       --no-remove-listing     不要删除‘.listing’文件。       --no-glob               不在 FTP 文件名中使用通配符展开。       --no-passive-ftp        禁用“passive”传输模式。       --retr-symlinks         递归目录时,获取链接的文件 (而非目录)。递归下载:  -r,  --recursive          指定递归下载。  -l,  --level=NUMBER       最大递归深度 (inf 或 0 代表无限制,即全部下载)。       --delete-after       下载完成后删除本地文件。  -k,  --convert-links      让下载得到的 HTML 或 CSS 中的链接指向本地文件。  -K,  --backup-converted   在转换文件 X 前先将它备份为 X.orig。  -m,  --mirror             -N -r -l inf --no-remove-listing 的缩写形式。  -p,  --page-requisites    下载所有用于显示 HTML 页面的图片之类的元素。       --strict-comments    用严格方式 (SGML) 处理 HTML 注释。递归接受/拒绝:  -A,  --accept=LIST               逗号分隔的可接受的扩展名列表。  -R,  --reject=LIST               逗号分隔的要拒绝的扩展名列表。  -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                 不追溯至父目录。

04. 参考示例

4.1 使用wget下载单个文件

[root@localhost test]# wget https://github.com/jindeng/sz14.git--2019-08-30 10:26:02--  https://github.com/jindeng/sz14.git正在解析主机 github.com (github.com)... 13.250.177.223正在连接 github.com (github.com)|13.250.177.223|:443... 已连接。已发出 HTTP 请求,正在等待回应... 301 Moved Permanently位置:https://github.com/jindeng/sz14 [跟随至新的 URL]--2019-08-30 10:26:03--  https://github.com/jindeng/sz14再次使用存在的到 github.com:443 的连接。已发出 HTTP 请求,正在等待回应... 200 OK长度:未指定 [text/html]正在保存至: “sz14.git”    [ <=>                                 ] 76,845      --.-K/s 用时 0.1s    2019-08-30 10:26:04 (710 KB/s) - “sz14.git” 已保存 [76845][root@localhost test]#

4.2 下载并以不同的文件名保存

[root@localhost test]# wget -O test.git https://github.com/jindeng/sz14.git--2019-08-30 10:27:38--  https://github.com/jindeng/sz14.git正在解析主机 github.com (github.com)... 13.250.177.223正在连接 github.com (github.com)|13.250.177.223|:443... 已连接。已发出 HTTP 请求,正在等待回应... 301 Moved Permanently位置:https://github.com/jindeng/sz14 [跟随至新的 URL]--2019-08-30 10:27:38--  https://github.com/jindeng/sz14再次使用存在的到 github.com:443 的连接。已发出 HTTP 请求,正在等待回应... 200 OK长度:未指定 [text/html]正在保存至: “test.git”    [  <=>                                ] 76,845       307KB/s 用时 0.2s   2019-08-30 10:27:39 (307 KB/s) - “test.git” 已保存 [76845][root@localhost test]#

4.3 wget限速下载

[root@localhost test]# wget --limit-rate=300k https://github.com/jindeng/sz14.git--2019-08-30 10:30:03--  https://github.com/jindeng/sz14.git正在解析主机 github.com (github.com)... 13.229.188.59正在连接 github.com (github.com)|13.229.188.59|:443... 已连接。已发出 HTTP 请求,正在等待回应... 301 Moved Permanently位置:https://github.com/jindeng/sz14 [跟随至新的 URL]--2019-08-30 10:30:04--  https://github.com/jindeng/sz14再次使用存在的到 github.com:443 的连接。已发出 HTTP 请求,正在等待回应... 200 OK长度:未指定 [text/html]正在保存至: “sz14.git”    [  <=>                                ] 76,831       308KB/s 用时 0.2s   2019-08-30 10:30:05 (308 KB/s) - “sz14.git” 已保存 [76831][root@localhost test]#

当你执行wget的时候,它默认会占用全部可能的宽带下载。但是当你准备下载一个大文件,而你还需要下载其它文件时就有必要限速了。

4.4 使用wget断点续传

[root@localhost test]# wget -c https://github.com/jindeng/sz14.git            --2019-08-30 10:30:30--  https://github.com/jindeng/sz14.git正在解析主机 github.com (github.com)... 13.229.188.59正在连接 github.com (github.com)|13.229.188.59|:443... 已连接。已发出 HTTP 请求,正在等待回应... 301 Moved Permanently位置:https://github.com/jindeng/sz14 [跟随至新的 URL]--2019-08-30 10:30:30--  https://github.com/jindeng/sz14再次使用存在的到 github.com:443 的连接。已发出 HTTP 请求,正在等待回应... 200 OK长度:未指定 [text/html]正在保存至: “sz14.git”    [ <=>                                 ] 76,831      --.-K/s 用时 0.1s    2019-08-30 10:30:31 (665 KB/s) - “sz14.git” 已保存 [76831][root@localhost test]#

使用wget -c重新启动下载中断的文件,对于我们下载大文件时突然由于网络等原因中断非常有帮助,我们可以继续接着下载而不是重新下载一个文件。需要继续中断的下载时可以使用-c参数。

4.5 使用wget后台下载

[root@localhost test]# wget -b https://github.com/jindeng/sz14.git继续在后台运行,pid 为 4592。将把输出写入至 “wget-log”。

对于下载非常大的文件的时候,我们可以使用参数-b进行后台下载,你可以使用以下命令来察看下载进度:

[root@localhost test]# tail -f wget-log 再次使用存在的到 github.com:443 的连接。已发出 HTTP 请求,正在等待回应... 200 OK长度:未指定 [text/html]正在保存至: “sz14.git”     0K .......... .......... .......... .......... ..........  106K    50K .......... .......... .....                            4.40M=0.5s2019-08-30 10:31:19 (157 KB/s) - “sz14.git” 已保存 [76831]

4.6 伪装代理名称下载

[root@localhost test]# wget --user-agent="Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16" https://github.com/jindeng/sz14.git--2019-08-30 10:35:46--  https://github.com/jindeng/sz14.git正在解析主机 github.com (github.com)... 13.229.188.59正在连接 github.com (github.com)|13.229.188.59|:443... 已连接。已发出 HTTP 请求,正在等待回应... 301 Moved Permanently位置:https://github.com/jindeng/sz14 [跟随至新的 URL]--2019-08-30 10:35:47--  https://github.com/jindeng/sz14再次使用存在的到 github.com:443 的连接。已发出 HTTP 请求,正在等待回应... 200 OK长度:未指定 [text/html]正在保存至: “sz14.git.1”    [   <=>                               ] 78,355       128KB/s 用时 0.6s   2019-08-30 10:35:48 (128 KB/s) - “sz14.git.1” 已保存 [78355][root@localhost test]#

有些网站能通过根据判断代理名称不是浏览器而拒绝你的下载请求。不过你可以通过--user-agent参数伪装。

4.7 测试下载链接

[root@localhost test]# wget --spider https://github.com/jindeng/sz14.git开启 Spider 模式。检查是否存在远程文件。--2019-08-30 10:37:47--  https://github.com/jindeng/sz14.git正在解析主机 github.com (github.com)... 52.74.223.119正在连接 github.com (github.com)|52.74.223.119|:443... 已连接。已发出 HTTP 请求,正在等待回应... 301 Moved Permanently位置:https://github.com/jindeng/sz14 [跟随至新的 URL]开启 Spider 模式。检查是否存在远程文件。--2019-08-30 10:37:47--  https://github.com/jindeng/sz14正在连接 github.com (github.com)|52.74.223.119|:443... 已连接。已发出 HTTP 请求,正在等待回应... 200 OK长度:未指定 [text/html]存在远程文件且该文件可能含有更深层的链接,但不能进行递归操作 -- 无法获取。[root@localhost test]#

当你打算进行定时下载,你应该在预定时间测试下载链接是否有效。我们可以增加--spider参数进行检查。

你可以在以下几种情况下使用--spider参数:

  • 定时下载之前进行检查
  • 间隔检测网站是否可用
  • 检查网站页面的死链接

4.8 增加重试次数

[root@localhost test]# wget --tries=40 https://github.com/jindeng/sz14.git--2019-08-30 10:40:10--  https://github.com/jindeng/sz14.git正在解析主机 github.com (github.com)... 52.74.223.119正在连接 github.com (github.com)|52.74.223.119|:443... 已连接。已发出 HTTP 请求,正在等待回应... 301 Moved Permanently位置:https://github.com/jindeng/sz14 [跟随至新的 URL]--2019-08-30 10:40:10--  https://github.com/jindeng/sz14再次使用存在的到 github.com:443 的连接。已发出 HTTP 请求,正在等待回应... 200 OK长度:未指定 [text/html]正在保存至: “sz14.git.2”    [   <=>                               ] 76,846       138KB/s 用时 0.5s   2019-08-30 10:40:11 (138 KB/s) - “sz14.git.2” 已保存 [76846][root@localhost test]#

如果网络有问题或下载一个大文件也有可能失败。wget默认重试20次连接下载文件。如果需要,你可以使用--tries增加重试次数。

4.9 下载多个文件

[root@localhost test]# wget -i filelist.txt

保存一份下载链接文件

filelist.txt文件内容就是下载链接地址

4.10 镜像网站

[root@localhost test]# wget --mirror -p --convert-links -P ./LOCAL URL[root@localhost test]#

下载整个网站到本地。

  • --miror开户镜像下载。
  • -p下载所有为了html页面显示正常的文件。
  • --convert-links下载后,转换成本地的链接。
  • -P ./LOCAL保存所有文件和目录到本地指定目录。

4.11 过滤指定格式下载

[root@localhost test]# wget --reject=gif https://github.com/jindeng/sz14.git[root@localhost test]#

下载一个网站,但你不希望下载图片,可以使用这条命令。

4.12 把下载信息存入日志文件

[root@localhost test]# wget -o download.log https://github.com/jindeng/sz14.git[root@localhost test]#

不希望下载信息直接显示在终端而是在一个日志文件,可以使用。

4.13 限制总下载文件大小

[root@localhost test]# wget -Q5m -i filelist.txt[root@localhost test]#

当你想要下载的文件超过5M而退出下载,你可以使用。注意:这个参数对单个文件下载不起作用,只能递归下载时才有效。

4.14下载指定格式文件

[root@localhost test]# wget -r -A.pdf url[root@localhost test]#

可以在以下情况使用该功能:

  • 下载一个网站的所有图片。
  • 下载一个网站的所有视频。
  • 下载一个网站的所有PDF文件。

4.15 使用wget匿名ftp下载

[root@localhost test]# wget ftp-url[root@localhost test]#

4.16 使用wget用户名和密码认证的ftp下载

[root@localhost test]# wget --ftp-user=USERNAME --ftp-password=PASSWORD url[root@localhost test]#

4.17 指定下载目录

[root@localhost test]# wget -P /home/download http://tel.mirrors.163.com/centos/6.4/isos/x86_64/CentOS-6.4-x86_64-bin-DVD1.iso

4.18 下载目录中所有的文件

[root@localhost test]# wget -r -np -nd http://tel.mirrors.163.com/centos/6.4/os/x86_64/

4.19 下载一个网站的本地镜像

[root@localhost test]# wget -c -r --level=1 -k -p -np http://docs.Python.org/2/tutorial/index.html

4.20 获取远程文件内容,以标准输出 + 管道方式直接运行

[root@localhost test]# wget -O- http://xxx.bashcommand.com/test.sh | sh

05. 附录

参考:

转载地址:https://dengjin.blog.csdn.net/article/details/100145281 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:【Linux】一步一步学Linux——curl命令(193)
下一篇:【Linux】一步一步学Linux——ipcalc命令(191)

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年04月13日 18时59分51秒