赞
踩
wget 命令
Linux provides different tools to download files via different types of protocols like HTTP, FTP, HTTPS, etc. wget is the most popular tool used to download files via a command-line interface. Wget is supported by Linux, BSD, Windows, MacOSX. Wget has a rich feature set some of them can be listed
Linux提供了不同的工具来通过不同类型的协议(例如HTTP,FTP,HTTPS等)下载文件。wget是用于通过命令行界面下载文件的最受欢迎的工具。 Wget受Linux,BSD,Windows,MacOSX支持。 Wget具有丰富的功能集,其中一些可以列出
Curl is alternative to wget. To read curl tutorial click this link
Help information of wget can be listed like below.
wget的帮助信息如下所示。
$ wget -h
The most common usage of wget command is without providing any option or parameter except the download URL. We will use wget only to provide the download URL.
wget命令最常见的用法是不提供任何选项或参数(下载URL除外)。 我们将仅使用wget提供下载URL。
$ wget http://ftp.gnu.org/gnu/wget/wget-1.19.tar.gz
While downloading the downloaded file is named the same as provided in the download URL. In the previous example, the file is named as wget-1.19.tar.gz
as the URL provides this name. We can change the saved file name different than the URL. We will use -O
option with name parameter to set the name as wget.tar.gz
and so will remove the version part from the file name.
下载时,下载文件的名称与下载URL中提供的名称相同。 在上一个示例中,该文件名为wget-1.19.tar.gz
因为URL提供了该名称。 我们可以更改保存的文件名,而不是URL。 我们将使用带名称参数的-O
选项将名称设置为wget.tar.gz
,因此将从文件名中删除版本部分。
$ wget -O wget.tar.gz http://ftp.gnu.org/gnu/wget/wget-1.19.tar.gz
There is an other useful feature of wget which gives us the ability to download multiple files. We will provide multiple URLs in a single command. This files will be downloaded and named as in the URL. There is no explicit option specification required. We just provide URLs in a row by separating them with spaces. There is no limit the count of URLs
wget的另一个有用功能是使我们能够下载多个文件。 我们将在一个命令中提供多个URL。 该文件将被下载并按照URL中的名称命名。 不需要明确的选项规范。 我们只是通过用空格分隔URL来连续提供URL。 网址数量没有限制
$ wget http://ftp.gnu.org/gnu/wget/wget-1.19.tar.gz http://ftp.gnu.org/gnu/wget/wget-1.18.tar.gz
In the previous example, we have downloaded multiple files by specifying the URLs from the command like. This may become difficult to manage if there are 100 URLs to download. Another situation may be that the URLs are provided externally in a plain text format line by line. Providing these URLs into the command line is a hard and error-prone job. Hopefully, wget have the feature to read URLs from a file line by line just specifying the file name. We will provide the URLs in a plain text file named downloads.txt
line by line with -i
option.
在前面的示例中,我们通过从like命令指定URL来下载了多个文件。 如果要下载100个URL,则可能难以管理。 另一种情况可能是,URL以纯文本格式逐行从外部提供。 在命令行中提供这些URL是一项艰巨且容易出错的工作。 希望wget具有仅指定文件名就可以逐行读取文件中URL的功能。 我们将使用-i
选项逐行提供纯文本文件中的URL,该文件名为downloads.txt
。
- http://ftp.gnu.org/gnu/wget/wget-1.19.tar.gz
- http://ftp.gnu.org/gnu/wget/wget-1.18.tar.gz
And we will download. It have very clear presentation.
然后我们将下载。 它有非常清晰的演示。
$ wget -i downloads.txt
Another great feature of the wget command is resuming downloads from where it left. Especially in big files, the download may be interrupted after %98 completion which is a nightmare. The -c
option is provided to resume the download without starting it from scratch.
wget命令的另一个重要功能是从其离开的位置继续下载。 特别是在大文件中,下载可能会在%98完成后中断,这是一场噩梦。 提供了-c
选项,可以继续下载而无需从头开始。
$ wget -c http://ftp.gnu.org/gnu/wget/wget-1.19.tar.gz
As we can see from the screenshot the downloaded part of the file is presented as +
plus sign in the download bar. Also, there is information about renaming length and already downloaded length with line starts Lenght:...
.There is also information like Partial Content
从屏幕截图中可以看到,文件的下载部分在下载栏中显示为+
加号。 此外,还有关于重命名长度和已经下载的长度(行开始Lenght:...
)的信息。还有诸如Partial Content
信息
By default wget do not overwrite to the file. If it see the same file name exists with the downloaded file it appends .1
to the end of downloaded file. This .1
is incremented if it is all ready exists.
默认情况下,wget不会覆盖该文件。 如果发现下载的文件存在相同的文件名,则将.1
附加到下载文件的末尾。 如果已准备就绪,则此.1
递增。
Wget starts as an interactive process for the download. During the download, the wget process remains a foreground process. But in some situations, we may need to send wget to the background. With -b
option the wget command will send to the background.
Wget作为下载的交互式过程开始。 在下载期间,wget进程仍然是前台进程。 但是在某些情况下,我们可能需要将wget发送到后台。 使用-b
选项,wget命令将发送到后台。
$ wget -b http://ftp.gnu.org/gnu/wget/wget-1.19.tar.gz
By default, the download speed of the wget will be unrestricted. So it will consume the bandwidth according to remote site upload speed. This behavior can be not suitable for some situations. We may want to not use whole bandwidth and remain bandwidth to the other critical applications. We can use --limit-rate
options with the bandwidth value. In this example, we set bandwidth as 50KB/s
.
默认情况下,wget的下载速度不受限制。 因此,它将根据远程站点上载速度消耗带宽。 此行为可能不适用于某些情况。 我们可能不希望使用整个带宽,而将带宽留给其他关键应用程序。 我们可以将--limit-rate
选项与带宽值一起使用。 在此示例中,我们将带宽设置为50KB/s
。
$ wget --limit-rate=50K http://ftp.gnu.org/gnu/wget/wget-1.19.tar.gz
As we can see down left corner the bandwidth is limited with 50KB/s
正如我们在左下角看到的,带宽限制为50KB / s
Security is an important issue nowadays. So FTP servers are not so secure but they try to implement some security steps like user name and password. wget can use FTP authentication with an FTP user name and password. We will use --ftp-user
for specifying the username and --ftp-password
to specify the FTP password.
安全是当今的重要问题。 因此,FTP服务器不是那么安全,但是它们尝试实现一些安全步骤,例如用户名和密码。 wget可以将FTP身份验证与FTP用户名和密码一起使用。 我们将使用--ftp-user
指定用户名,并使用--ftp-password
指定FTP密码。
$ wget --ftp-user=anonymous --ftp-password=mypass ftp.gnu.org/gnu/wget/wget-1.19.tar.gz
Like in the previous example we can specify the HTTP username and password. We will use --http-user
to specify HTTP user name and --http-password
for the HTTP password.
像前面的示例一样,我们可以指定HTTP用户名和密码。 我们将使用--http-user
指定HTTP用户名,并使用--http-password
作为HTTP密码。
$ wget --http-user=anonymous --http-password=mypass https://ftp.gnu.org/gnu/wget/wget-1.19.tar.gz
While connecting web pages HTTP protocol provides information about the user browser. The same information is provided while downloading files with wget. Some web sites can deny access to the non-standard browser. Wget provides user agent different from standard browsers by default. This browser information can be provided with --user-agent
parameter with the name of the browser. In this example, we will provide the browser information as Mozilla Firefox
.
连接网页时,HTTP协议提供有关用户浏览器的信息。 使用wget下载文件时,将提供相同的信息。 某些网站可以拒绝访问非标准浏览器。 Wget默认提供与标准浏览器不同的用户代理。 可以通过带有浏览器名称的--user-agent
参数提供此浏览器信息。 在此示例中,我们将以Mozilla Firefox
提供浏览器信息。
- $ wget --user-agent="Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008092416 Firefox/39
- " https://ftp.gnu.org/gnu/wget/wget-1.19.tar.gz
Another useful feature of the wget is testing URLs before downloading them. This will give some hints about the URL and file status. We will use --spider
options to check if a remote file exists and downloadable.
wget的另一个有用功能是在下载URL之前先对其进行测试。 这将提供有关URL和文件状态的一些提示。 我们将使用--spider
选项检查远程文件是否存在并可以下载。
$ wget --spider https://ftp.gnu.org/gnu/wget/wget-1.19.tar.gz
As we can see from screenshot there is a message expressing Remote file exists.
in the last line. We can also get the size of the file without downloading.
从屏幕截图中可以看到,有一条消息表示Remote file exists.
在最后一行。 我们也可以不下载而获得文件的大小。
In problematic networks or download servers, there may be some download problems. The most known problem is can not access the remote file for short periods. We can set retry count which will try to download for specified times. --tries
options with the retry count can be used.
在有问题的网络或下载服务器中,可能存在一些下载问题。 最知名的问题是短期内无法访问远程文件。 我们可以设置重试次数,该次数将尝试下载指定的时间。 可以使用带有重试计数的--tries
选项。
$ wget --tries=10 https://ftp.gnu.org/gnu/wget/wget-1.19.tar.gz
There are different tools to download the whole site just providing the homepage URL. wget has this ability too. This command will spider and download all pages about this URL and subpages. This will make the site offline accessible. We will use --mirror
to download the whole site and provide the location to download with -P
parameter. Here we want to download the www.poftut.com
and its subpages
有多种工具可以下载整个站点,而仅提供主页URL。 wget也具有此功能。 此命令将搜寻并下载有关此URL和子页面的所有页面。 这将使站点可以脱机访问。 我们将使用--mirror
下载整个站点,并使用-P
参数提供下载位置。 在这里,我们要下载www.poftut.com
及其子页面
$ wget --mirror https://www.poftut.com -P poftut.com
While downloading multiple files or mirroring a site we may want to only download a specific file or file extension. This can be specified with -A
and extension or some part of the file name. In this example, we only want to download the .txt
files.
在下载多个文件或镜像站点时,我们可能只希望下载特定文件或文件扩展名。 可以使用-A
和扩展名或文件名的一部分来指定。 在此示例中,我们只想下载.txt
文件。
$ wget -A '.txt' --mirror https://www.poftut.com
While downloading multiple URLs or mirroring a site there will be a lot of files we do not want to download or too big to download. We will use –reject option by providing extensions or file names.
在下载多个URL或镜像站点时,会有很多我们不想下载的文件或太大而无法下载。 我们将通过提供扩展名或文件名来使用–reject选项。
$ wget --reject '.txt' --mirror https://www.poftut.com
By default logs created by wget are printed to the standard output which is generally the command line interface we are using. But using remotely or as a batch or background process, we can not get logs directly. so writing the logs to file is the best solution. We will use -o
option with the log file name.
默认情况下,由wget创建的日志会打印到标准输出,这通常是我们使用的命令行界面。 但是,使用远程或作为批处理或后台进程,我们无法直接获取日志。 因此将日志写入文件是最好的解决方案。 我们将-o
选项与日志文件名一起使用。
$ wget -o wget.log http://ftp.gnu.org/gnu/wget/wget-1.19.tar.gz
Another good option to set limit total downloaded file size is -Q
. We will set download size as 2 MB
in the example. This setting is not effective for single file download. It will effect for recursive or mirroring of sites.
设置限制总下载文件大小的另一个不错的选择是-Q
。 在示例中,我们将下载大小设置为2 MB
。 此设置对单文件下载无效。 这将对站点的递归或镜像产生影响。
$ wget -Q5m https://www.poftut.com
Version information about the wget command can be get with --version
option.
可以使用--version
选项获取有关wget命令的版本信息。
$ wget --version
翻译自: https://www.poftut.com/wget-command-tutorial-examples-linux/
wget 命令
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。