wget 命令的作用是通过 HTTP 和 FTP 方式从服务器上检索和下载文件,该命令可以浏览 WEB 站点上的文件,并将这些文件下载到本地机器。
wget 命令不提供用户交互式操作界面,但是可以在用户登录系统的情况下在后台运行,因此可以后台并在空闲或固定的时间下载网络资源。
wget [OPTION]... [URL]...
-V, --version 显示wget的版本信息
-h, --help 打印帮助信息
-b, --background 命令启动后,wget进入后台下载,如:wget -b http://***.zip
-O 以其他名称保存下载的文件,如:wget -O myName.zip http://***.zip
-P 将文件下载到指定目录,默认情况下,wget 将下载的文件保存在当前工作目录中,使用 -P 选项可以将文件保存到指定目录下。如:wget -P /root/tmp http://***.zip
-c 断点续传,当我们下载一个大文件时,如果中途网络断开导致没有下载完成,我们就可以使用命令的 -c 选项恢复下载,让下载从上次没有下载完的位置开始,无需完全重新下载。
-i 一次下载多个文件,如果想要一次下载多个文件,首先需要创建一个文本文件,并将所有的要下载的 url 添加到该文件中,每个 url 都必须是单独的一行。然后使用 -i 命令下载文件中的多个 url。如:wget -i url_lists.txt
--limit-rate 限制下载速度。默认情况下,wget 命令会以全速下载,但是有时下载一个非常大的资源的话,可能会占用大量的可用带宽,影响其他使用网络的任务,这时就要限制下载速度。如:wget --limit-rate=1m http://***.zip 该命令将下载速度限制为 1m/s。
-U 设定模拟下载。如果远程服务器阻止wget下载资源,我们可以通过 -U 选项模拟浏览器进行下载。如:wget -U 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.43 Safari/537.36' https://***.zip 该命令将模拟 Chrome 浏览器去下载资源。
--tries 设置重试次数。如果网络有问题或下载一个大文件有可能会下载失败,wget 默认重试 20 次,我们可以使用 -tries选项来设置重试次数。如:wget -tries 5 http://***.zip 设置重试 5 次。
(1)下载 www.hxstrive.com 网站首页,命令如下:
[root@hxstrive ~]# wget http://www.hxstrive.com --2022-04-18 23:00:24-- http://www.hxstrive.com/ Resolving www.hxstrive.com (www.hxstrive.com)... 39.99.59.199 Connecting to www.hxstrive.com (www.hxstrive.com)|39.99.59.199|:80... connected. HTTP request sent, awaiting response... 301 Moved Permanently Location: https://www.hxstrive.com/ [following] --2022-04-18 23:00:24-- https://www.hxstrive.com/ Connecting to www.hxstrive.com (www.hxstrive.com)|39.99.59.199|:443... connected. HTTP request sent, awaiting response... 200 OK Length: unspecified [text/html] Saving to: ‘index.html’ index.html [ <=> ] 55.12K --.-KB/s in 0.05s 2022-04-18 23:00:24 (1.11 MB/s) - ‘index.html’ saved [56439]
(2)最多尝试 4 次下载图片文件,命令如下:
[root@hxstrive ~]# wget -t 4 https://www.hxstrive.com/hxstrivedocs/2019/06/14/e85b37606d89418eb3b30fa44c00493a-574x335.png --2022-04-18 23:21:14-- https://www.hxstrive.com/hxstrivedocs/2019/06/14/e85b37606d89418eb3b30fa44c00493a-574x335.png Resolving www.hxstrive.com (www.hxstrive.com)... 39.99.59.199 Connecting to www.hxstrive.com (www.hxstrive.com)|39.99.59.199|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 53759 (52K) [image/png] Saving to: ‘e85b37606d89418eb3b30fa44c00493a-574x335.png’ e85b37606d89418eb3b30fa44c00493a-574 100%[=====================================================================>] 52.50K --.-KB/s in 0.04s 2022-04-18 23:21:14 (1.15 MB/s) - ‘e85b37606d89418eb3b30fa44c00493a-574x335.png’ saved [53759/53759]
(3)下载图片文件,且使用 -o 参数将图片保存为 my.png,命令如下:
[root@hxstrive ~]# wget -o my.png https://www.hxstrive.com/hxstrivedocs/2019/06/14/e85b37606d89418eb3b30fa44c00493a-574x335.png [root@hxstrive ~]# ll my.png -rw-r--r--. 1 root root 671 Apr 18 23:25 my.png
(4)将图片下载到 /root/tmp 目录,命令如下:
[root@hxstrive ~]# wget -P /root/tmp https://www.hxstrive.com/hxstrivedocs/2019/06/14/e85b37606d89418eb3b30fa44c00493a-574x335.png --2022-04-19 23:23:00-- https://www.hxstrive.com/hxstrivedocs/2019/06/14/e85b37606d89418eb3b30fa44c00493a-574x335.png Resolving www.hxstrive.com (www.hxstrive.com)... 39.99.59.199 Connecting to www.hxstrive.com (www.hxstrive.com)|39.99.59.199|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 53759 (52K) [image/png] Saving to: ‘/root/tmp/e85b37606d89418eb3b30fa44c00493a-574x335.png’ e85b37606d89418eb3b30fa44c00493a 100%[========================================================>] 52.50K --.-KB/s in 0.04s 2022-04-19 23:23:01 (1.18 MB/s) - ‘/root/tmp/e85b37606d89418eb3b30fa44c00493a-574x335.png’ saved [53759/53759] [root@hxstrive ~]# ll /root/tmp/ total 56 -rw-r--r--. 1 root root 53759 Apr 12 2020 e85b37606d89418eb3b30fa44c00493a-574x335.png
(5)一次下载多个文件,命令如下:
[root@hxstrive ~]# cat url_list.txt https://www.hxstrive.com/hxstrivedocs/2018/03/09/58e21eadc6934387a7eeb5730fd05577-574x335.png https://www.hxstrive.com/hxstrivedocs/2019/06/14/e85b37606d89418eb3b30fa44c00493a-574x335.png [root@hxstrive ~]# wget -i url_list.txt --2022-04-19 23:26:00-- https://www.hxstrive.com/hxstrivedocs/2018/03/09/58e21eadc6934387a7eeb5730fd05577-574x335.png Resolving www.hxstrive.com (www.hxstrive.com)... 39.99.59.199 Connecting to www.hxstrive.com (www.hxstrive.com)|39.99.59.199|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 33221 (32K) [image/png] Saving to: ‘58e21eadc6934387a7eeb5730fd05577-574x335.png’ 58e21eadc6934387a7eeb5730fd05577 100%[========================================================>] 32.44K --.-KB/s in 0.001s 2022-04-19 23:26:01 (57.4 MB/s) - ‘58e21eadc6934387a7eeb5730fd05577-574x335.png’ saved [33221/33221] --2022-04-19 23:26:01-- https://www.hxstrive.com/hxstrivedocs/2019/06/14/e85b37606d89418eb3b30fa44c00493a-574x335.png Reusing existing connection to www.hxstrive.com:443. HTTP request sent, awaiting response... 200 OK Length: 53759 (52K) [image/png] Saving to: ‘e85b37606d89418eb3b30fa44c00493a-574x335.png.2’ e85b37606d89418eb3b30fa44c00493a 100%[========================================================>] 52.50K --.-KB/s in 0.05s 2022-04-19 23:26:01 (1.09 MB/s) - ‘e85b37606d89418eb3b30fa44c00493a-574x335.png.2’ saved [53759/53759] FINISHED --2022-04-19 23:26:01-- Total wall clock time: 0.7s Downloaded: 2 files, 85K in 0.05s (1.74 MB/s)
更多关于命令详细参考手册,请使用 man 命令或者 --help 参数获取帮助信息