Linux 基础教程 38-文件下载

什么是wget

    wget用原始帮助里面的英文来讲就是:The non-interactive network downloader,非交互式网络下载器。它支持HTTP、HTTPS、FTP等协议,同时也支持基于HTTP代理的下载。其他主要特点如下所示:

  • 可以工作于后台,而不需要登录到系统
  • 可以跟踪HTML、XHTML、CSS等文件,从而将服务器上的页面下载到本地进行离线浏览。
  • 支持慢速和不稳定网络下的下载和连接。

常用语法

wget [选项] [URL]

常用选项参数如下所示:

选项 说明
-V,--version 显示版本信息
-h,--help 显示帮助信息
-b,--backgroup 以后台形式进行运行
-e command,--execute command 如果命令属于.wgetrc,则执行命令
-o logfile,--output-file=logfile 将日志打印到文件中
-a logfile,--append-output=logfile 追加日志到文件中
-q,--quiet 静默模式,关闭wget的输出信息
-v,--verbose 显示详细的信息,wget默认模式
-i file,--inpu-file=file 从文件中读取URL下载地址
-B URL,--base=URL 设置连接的相对URL地址
--bind-address=ADDRESS 设置连接的IP地址或主机名
-t number,--tries=number 设置重试次数,0和inf则代表一直重试,默认值为20
-O file,--output-document=file 将下载文件中的内容保存到本地文件中
-nc,--no-clobber 如果下载文件已经存在,则不下载
-c,--continue 继续执行上次的下载任务
-N,--timestamping 增加时间戳
-S,--server-response 打印HTTP、FTP头部信息
-T seconds,--timeout=seconds 设置超时时间
--limit-rate=amount 限制下载速度
-X 设置下载排除项
-r,--recursive 递归下载文件
-nH, --no-host-directories 禁止创建以域名命令的文件夹
--cut-dirs=number 忽略下载地址中的层次目录

配置文件

    要想好好使用wget命令,还是先了解一下wget隐藏的一些潜规则,位置在:

  • /etc/wgetrc
  • 家目录中的.wgetrc

1、wget命令有一些选项可以使用逗号进行隔开,如-X可以设置排除项,如果仅仅是一次,则无所谓,但如果长期均不需要下载这些目录,则可以将这些排除项添加.wgetrc配置文件中

exclude_directories=test,tmp,temp

2、在第一种情况下,突然又想下载这些文件时,可以去掉配置文件中设置,也可以采用以下的方法:

wget -r -X '' ftp://192.168.8.8

常用示例

1、下载文件:

[root@localhost ]# wget https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_redBlue_32fe2c69.png
--2018-07-28 19:55:17--  https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_redBlue_32fe2c69.png
正在解析主机 ss0.bdstatic.com (ss0.bdstatic.com)... 180.163.198.32
正在连接 ss0.bdstatic.com (ss0.bdstatic.com)|180.163.198.32|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:8618 (8.4K) [image/png]
正在保存至: “logo_redBlue_32fe2c69.png”

100%[=============================================================================================================================>] 8,618       --.-K/s 用时 0.002s

2018-07-28 19:55:17 (3.80 MB/s) - 已保存 “logo_redBlue_32fe2c69.png” [8618/8618])

2、下载目录和后台执行

[root@localhost test]# wget -r -b https://mirrors.aliyun.com/centos/7.5.1804/atomic/x86_64/adb/
继续在后台运行,pid 为 26252。
将把输出写入至 “wget-log”。
[root@localhost test]# ll
总用量 4
drwxr-xr-x 3 root root   38 7月  28 20:14 mirrors.aliyun.com
-rw-r--r-- 1 root root 1257 7月  28 20:14 wget-log
[root@localhost test]# cat wget-log 
--2018-07-28 20:14:11--  https://mirrors.aliyun.com/centos/7.5.1804/atomic/x86_64/adb/
正在解析主机 mirrors.aliyun.com (mirrors.aliyun.com)... 180.163.155.11, 180.163.155.8, 180.163.155.9, ...
正在连接 mirrors.aliyun.com (mirrors.aliyun.com)|180.163.155.11|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:3518 (3.4K) [text/html]
正在保存至: “mirrors.aliyun.com/centos/7.5.1804/atomic/x86_64/adb/index.html”

3、禁止生成下载地址中的多级目录

[root@localhost test]# wget -b -r -nH --cut-dir=6 https://mirrors.aliyun.com/centos/7.5.1804/atomic/x86_64/adb/
继续在后台运行,pid 为 34849。
将把输出写入至 “wget-log”。
[root@localhost test]# ll
总用量 12
-rw-r--r-- 1 root root 3518 7月  28 20:25 index.html
-rw-r--r-- 1 root root  295 10月 31 2017 robots.txt
-rw-r--r-- 1 root root 1113 7月  28 20:25 wget-log

下载目录路径如下所示:

情况 结果
-r mirrors.aliyun.com/centos/7.5.1804/atomic/x86_64/adb/
-nH centos/7.5.1804/atomic/x86_64/adb/
-nH --cut-dir=1 centos/7.5.1804/atomic/x86_64/adb/
-nH --cut-dir=4 adb/
-nH --cut-dir=6 .

4、将下载的文件保存到本地文件中

[root@localhost test]# wget -t 2 -o log.txt https://mirrors.aliyun.com/centos/7.5.1804/isos/x86_64/sha256sum.txt  -O test.txt
[root@localhost test]# ll
总用量 8
-rw-r--r-- 1 root root 554 7月  28 20:37 log.txt
-rw-r--r-- 1 root root 598 5月  10 03:59 test.txt
[root@localhost test]# cat log.txt
--2018-07-28 20:37:28--  https://mirrors.aliyun.com/centos/7.5.1804/isos/x86_64/sha256sum.txt
正在解析主机 mirrors.aliyun.com (mirrors.aliyun.com)... 180.163.155.9, 180.163.155.10, 180.163.155.11, ...
正在连接 mirrors.aliyun.com (mirrors.aliyun.com)|180.163.155.9|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:598 [text/plain]
正在保存至: “test.txt”
     0K                    100%  175M=0s
2018-07-28 20:37:28 (175 MB/s) - 已保存 “test.txt” [598/598])

[root@localhost test]# cat test.txt
506e4e06abf778c3435b4e5745df13e79ebfc86565d7ea1e128067ef6b5a6345  CentOS-7-x86_64-DVD-1804.iso
b346daae2a93caed88e822e722e7284c648f9919d475ff98489b424350f99a45  CentOS-7-x86_64-LiveGNOME-1804.iso
040ddfb27d30e48efad8709c9df946202cec169077c843fd2cbe8d802187ff8e  CentOS-7-x86_64-LiveKDE-1804.iso
714acc0aefb32b7d51b515e25546835e55a90da9fb00417fbee2d03a62801efd  CentOS-7-x86_64-Minimal-1804.iso
99723c8b87dcec21df8aed23de1eb810346d42cfd2a3fafafe70a68296053417  CentOS-7-x86_64-Everything-1804.iso
937bf0a7b0932817f84f7230f15ed88911bbbd85c0c958680792b7f8d8f9c1a9  CentOS-7-x86_64-NetInstall-1804.iso

本文同步在微信订阅号上发布,如各位小伙伴们喜欢我的文章,也可以关注我的微信订阅号:woaitest,或扫描下面的二维码添加关注:
MyQRCode.jpg

猜你喜欢

转载自www.cnblogs.com/surpassme/p/9495804.html