1.介绍
wget 是一个从网络上自动下载文件的自由工具,支持通过 HTTP、HTTPS、FTP 三个最常见的 TCP/IP协议下载,并可以使用 HTTP 代理。
“wget” 这个名称来源于 “World Wide Web” 与 “get” 的结合。
所谓自动下载,是指 wget 可以在用户退出系统的之后在继续后台执行,直到下载任务完成。
2.用法
基本用法格式:
wget [OPTION] [URL]
在linux下,可以终端输入命令$ wget --help | more
查看其详细用法。
$ wget --help | more
GNU Wget 1.19.4, a non-interactive network retriever.
Usage: wget [OPTION]... [URL]...
Mandatory arguments to long options are mandatory for short options too.
Startup:
-V, --version display the version of Wget and exit
-h, --help print this help
-b, --background go to background after startup
-e, --execute=COMMAND execute a `.wgetrc'-style command
Logging and input file:
-o, --output-file=FILE log messages to FILE
-a, --append-output=FILE append messages to FILE
-d, --debug print lots of debugging information
-q, --quiet quiet (no output)
-v, --verbose be verbose (this is the default)
-nv, --no-verbose turn off verboseness, without being quiet
--report-speed=TYPE output bandwidth as TYPE. TYPE can be bits
-i, --input-file=FILE download URLs found in local or external FILE
-F, --force-html treat input file as HTML
-B, --base=URL resolves HTML input-file links (-i -F)
relative to URL
--config=FILE specify config file to use
--no-config do not read any config file
--rejected-log=FILE log reasons for URL rejection to FILE
Download:
-t, --tries=NUMBER set number of retries to NUMBER (0 unlimits)
--retry-connrefused retry even if connection is refused
-O, --output-document=FILE write documents to FILE
-nc, --no-clobber skip downloads that would download to
existing files (overwriting them)
--no-netrc don't try to obtain credentials from .netrc
也可以参考:
Linux wget命令详解 wget下载工具用法详解(附实例说明)
linux下载文件命令 wget 介绍
Linux的wget命令详解
这几篇文章对wget命令进行了比较详细的介绍。
wget功能十分强大,下面简单讲几个常用的用法。
2.1 wget下载单个文件
从网络下载一个文件并保存在当前目录。在下载的过程中会显示进度条,包含下载完成百分比,已经下载的字节,当前下载速度,剩余下载时间。
$ wget http://releases.ubuntu.com/18.04/ubuntu-18.04.5-desktop-amd64.iso
2.2 wget -P下载到自定义路径
可以将文件下载到自己指定的路径下,而不是当前路径。
用法:
$ wget -P PATH url
比如下载到/home/myfile
路径下:
$ wget -P /home/myfile http://releases.ubuntu.com/18.04/ubuntu-18.04.5-desktop-amd64.iso
2.3 wget -O下载并自定义文件名保存
wget默认会以最后一个符号”/”的后面的字符来命名下载文件,对于动态链接的下载通常文件名会不正确。
- 错误:下面的例子会下载一个文件并以名称download.php?id=1080保存,即使下载的文件是zip格式,它仍然以download.php?id=1080命名。
$ wget http://www.centos.bz/download?id=1
- 正确:为了解决这个问题,我们可以使用参数-O来指定一个文件名wordpress.zip:
$ wget -O wordpress.zip http://www.centos.bz/download.php?id=1080
2.4 wget -c断点续传
使用wget -c重新启动下载中断的文件,对于我们下载大文件时突然由于网络等原因中断非常有帮助,我们可以继续接着下载而不是重新下载一个文件。需要继续中断的下载时可以使用-c参数。
$ wget -c http://releases.ubuntu.com/18.04/ubuntu-18.04.5-desktop-amd64.iso
2.5 wget -b后台下载
对于下载非常大的文件的时候,我们可以使用参数-b进行后台下载。
$ wget -b http://releases.ubuntu.com/18.04/ubuntu-18.04.5-desktop-amd64.iso
Continuing in background, pid 1840.
Output will be written to `wget-log’.
可以使用以下命令来察看下载进度:
$ tail -f wget-log
2.6 wget -i下载多个文件
当要下载的文件数量较多时,可以使用
$ wget -i url_list.txt
来批量下载多个文件。
比如现在你有要下载三个ubuntu安装镜像文件,其url分别为:
http://releases.ubuntu.com/20.04/ubuntu-20.04.1-desktop-amd64.iso
http://releases.ubuntu.com/18.04/ubuntu-18.04.5-desktop-amd64.iso
http://releases.ubuntu.com/16.04/ubuntu-16.04.7-desktop-amd64.iso
现在将其分三行写入一个文本文件ubuntu_iso_url.txt
查看有无错误:
$ cat ubuntu_iso_url.txt
http://releases.ubuntu.com/20.04/ubuntu-20.04.1-desktop-amd64.iso
http://releases.ubuntu.com/18.04/ubuntu-18.04.5-desktop-amd64.iso
http://releases.ubuntu.com/16.04/ubuntu-16.04.7-desktop-amd64.iso
没有问题就命令行输入:
$ wget -i ubuntu_iso_url.txt