wget是一种功能强大的实用程序,它可以检索作为HTTP或FTP资源访问的文件,并将其保存到本地文件系统。wget提供了许多选项,允许用户配置如何下载和保存资源。它还具有递归下载功能,允许您下载一组链接的资源以供离线使用。
wget命令采用以下形式之一:
wget [OPTIONS] [URL]
wget [URL] [OPTIONS]
当在命令行发出没有选项时,wget将下载指定的文件[URL]到当前目录。请考虑以下示例:
$ wget http://www.linode.com/docs/assets/695-wget-example.txt
--2010-10-01 12:01:22-- http://www.linode.com/docs/assets/695-wget-example.txt
Resolving linode.com/docs... 74.207.233.254
Connecting to linode.com/docs|74.207.233.254|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 477 [text/plain]
Saving to: `wget-example.txt'
100%[================================================>] 477 --.-K/s in 0.002s
2010-10-01 12:01:48 (261 KB/s) - `wget-example.txt' saved [477/477]
$ cat wget-example.txt
This is an example resource for the `wget` document
<http://www.linode.com/docs/tools-reference/tools/download-resources-from-the-command-line-with-wget>, located
in the Linode Docs.
There are four lines of random characters at the end of this file.
y7tWn6zZRFAX1cXyQzzSBhTDC+/SpN/RezhI2acW3qr3HGFDCM7PX9frUhna75wG
6lOvibL5/sHTKP8N7tRfszZq1MaGlmpeEQN1n5afK6Awh0rykc5FMn2xb3jf0klF
wVPjuxsptT/L05K6avRI81Edg2+8CkS8uA16u+bXqRn1BBQutRvxwrWwrKuP10pR
uCf3HehndIeRghOAmXPc61cfUrHZ+MEqXYmSoKw4E0hI7GWXkwAyByCFPBVB9Fbe
此文档指定URL wget 之前的所有选项。但是,在URL参数wget之前和之后接受这些选项。两种形式在功能上是等同的。
-O选项控制wget写入下载内容的文件的位置和名称。如果您将文件名指定为 -, wget -O -,wget则会将下载的资源输出到终端。添加-q选项以抑制 wget状态输出如下:
$ wget -q -O - http://www.linode.com/docs/assets/695-wget-example.txt
This is an example resource for the `wget` document
<http://www.linode.com/docs/tools-reference/tools/download-resources-from-the-command-line-with-wget>, located
in the Linode Docs.
There are four lines of random characters at the end of this file.
y7tWn6zZRFAX1cXyQzzSBhTDC+/SpN/RezhI2acW3qr3HGFDCM7PX9frUhna75wG
6lOvibL5/sHTKP8N7tRfszZq1MaGlmpeEQN1n5afK6Awh0rykc5FMn2xb3jf0klF
wVPjuxsptT/L05K6avRI81Edg2+8CkS8uA16u+bXqRn1BBQutRvxwrWwrKuP10pR
uCf3HehndIeRghOAmXPc61cfUrHZ+MEqXYmSoKw4E0hI7GWXkwAyByCFPBVB9Fbe
要查看附加到资源的HTTP头信息,请使用该-S标志。标题信息通常有助于诊断Web服务器配置的问题。
$ wget -S http://www.linode.com/docs/assets/695-wget-example.txt
--2010-10-01 12:03:50-- http://www.linode.com/docs/assets/695-wget-example.txt
Resolving linode.com/docs... 74.207.233.254
Connecting to linode.com/docs|74.207.233.254|:80... connected.
HTTP request sent, awaiting response...
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 01 Oct 2010 16:03:51 GMT
Content-Type: text/plain
Content-Length: 477
Last-Modified: Fri, 01 Oct 2010 16:00:34 GMT
Connection: keep-alive
Accept-Ranges: bytes
Length: 477 [text/plain]
Saving to: `wget-example.txt'
100%[================================================>] 477 --.-K/s in 0s
2010-10-01 12:03:40 (1.73 MB/s) - `wget-example.txt' saved [477/477]
如果您需要访问需要HTTP身份验证的资源,请使用以下格式:
wget --http-user=[USERNAME] --http-password=[PASSWORD] [URL]
最佳实践:
wget --http-user=username --http-password=iwLcis4TMOgn3PTy http://example.com/backups/database.1285770344
除非Web服务器提示, wget一般不会发送认证信息。使用该**--auth-no-challenge**选项,wget在每种情况下强制发送身份验证凭据。
要成功下载使用签名SSL证书保护的资源,您必须指定该--no-check-certificate选项。
wget --no-check-certificate https://infra.example.com/backups/database-1285771361
信息仍然加密,但证书的真实性未被确认。
-r选项允许wget递归下载资源,搜索该内容以获取其他资源的链接,然后下载这些资源。这对于创建静态网站的备份或可用资源的快照非常有用。有各种其他选项来控制递归下载的行为。请考虑以下示例:
wget -r -l 3 -k -p -H https://example.com/
这些选项-r -l 3 -k -p -H具有以下功能:
wget将只下载链接到的资源。可用但未链接的资源将不会被下载。
如果您不想wget占用终端进程,请使用 -b 选项来背景下载过程。
$ wget -b http://www.linode.com/docs/assets/695-wget-example.txt
Continuing in background, pid 9810.
Output will be written to `wget-log'.
$ cat wget-log
--2010-10-01 12:06:55-- http://www.linode.com/docs/assets/695-wget-example.txt
Resolving linode.com/docs... 74.207.233.254
Connecting to linode.com/docs|74.207.233.254|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 477 [text/plain]
Saving to: `wget-example.txt'
0K 100% 686K=0.001s
2010-10-01 12:06:37 (686 KB/s) - `wget-example.txt' saved [477/477]
wget包括许多旨在节省带宽和冗余操作的选项。
如果您需要控制多少带宽wget使用,您可以使用--limit-rate=[RATE]选项指定“速率限制” 。[RATE]以秒为单位指定,除非k附加a 指定千字节。
$ wget --limit-rate=3k http://example.com/releases/1285786486.tar.gz
此命令下载1285786486.tar.gz文件,操作限制为每秒消耗不超过3千字节。用于限制下载的方法对于较大的文件而言比对快速完成的小型下载更有效。