Linux must know the wget command

table of Contents

Preface

In Linux, we often need to download software, and wget is a very suitable command for downloading files.

wget

wget is a free tool that automatically downloads files from the Internet. It supports downloading through the three most common TCP/IP protocols, HTTP, HTTPS, and FTP, and can use HTTP proxy. The name "wget" comes from the combination of "World Wide Web" and "get". The so-called automatic download means that wget can continue to execute in the background after the user exits the system until the download task is completed. wget is often used to download files from a specified URL.

grammar

wget(选项)(参数)

Options

-a<日志文件>:在指定的日志文件中记录资料的执行过程;
-A<后缀名>:指定要下载文件的后缀名,多个后缀名之间使用逗号进行分隔;
-b:进行后台的方式运行wget;
-B<连接地址>:设置参考的连接地址的基地地址;
-c:继续执行上次终端的任务;
-C<标志>:设置服务器数据块功能标志on为激活,off为关闭,默认值为on;
-d:调试模式运行指令;
-D<域名列表>:设置顺着的域名列表,域名之间用“,”分隔;
-e<指令>:作为文件“.wgetrc”中的一部分执行指定的指令;
-h:显示指令帮助信息;
-i<文件>:从指定文件获取要下载的URL地址;
-l<目录列表>:设置顺着的目录列表,多个目录用“,”分隔;
-L:仅顺着关联的连接;
-r:递归下载方式;
-nc:文件存在时,下载文件不覆盖原有文件;
-nv:下载时只显示更新和出错信息,不显示指令的详细执行过程;
-q:不显示指令执行过程;
-nh:不查询主机名称;
-v:显示详细执行过程;
-V:显示版本信息;
--passive-ftp:使用被动模式PASV连接FTP服务器;
--follow-ftp:从HTML文件中下载FTP连接文件。
-O,  --output-document=FILE    将文档写入 FILE。
-P,  --directory-prefix=PREFIX  以 PREFIX/... 保存文件
-x,  --force-directories        强制创建目录。

parameter

URL:下载文件的URL地址。

Instance

wget  https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz

The above example will download a file from the network and save it in the current directory

If you want to download with a specific file name, you can use the -O parameter, followed by our custom file name.

Example:

wget  -O mysql.tar.gz  https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz

The file downloaded in the above example will be saved with our defined mysql.tar.gz instead of the string at the last layer of the path (mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz)

Use wget to resume uploading

wget -c https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz

Using wget -c to restart downloading interrupted files is very helpful for us to download large files suddenly interrupted due to network and other reasons. We can continue to download instead of downloading a file again. You can use the -c parameter when you need to continue the interrupted download.

Use wget background download

wget -b https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz

For downloading very large files, we can use the parameter -b to download in the background. You can use the following command to view the download progress:

tail -f wget-log

Download the file to the specified directory

Sometimes, we do not want to download the file to the current directory, then we can use the -P parameter to specify the download directory

wget --P  /root/data  https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz

The files in the above example will be downloaded to the /root/data directory

Test download link

When you plan to use wget to download, we can add the –spider parameter to check whether the download link is valid.

wget --spider URL

Example:

 wget --spider https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz

If the link is valid, the following similar information will be returned (200 OK)

正在解析主机 cdn.mysql.com (cdn.mysql.com)... 23.209.4.33
正在连接 cdn.mysql.com (cdn.mysql.com)|23.209.4.33|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:644930593 (615M) [application/x-tar-gz]
存在远程文件。

Limit download speed

Wget may take over our network. If we don’t want wget to use all the network, we can use wget --limit-rate to limit the speed.

Example: 300k speed limit

wget --limit-rate=300k  https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz

FTP download

Use wget anonymous ftp download:

wget ftp-url

FTP download using wget username and password authentication:

wget --ftp-user=USERNAME --ftp-password=PASSWORD url

Guess you like

Origin blog.csdn.net/qq_36551991/article/details/110834348