分享最新优惠信息
购买主机更加划算

分享整理Linux系统中10个Wget(Linux文件下载器)命令示例

众所周知,Wget是一个免费的命令行实用程序和网络文件下载器,它具有许多使文件下载变得容易的功能,主要包括:

  • 下载大文件或镜像完整的网站或FTP站点。
  • 一次下载多个文件。
  • 为下载设置带宽和速度限制。
  • 通过代理下载文件。
  • 可以恢复中止的下载。
  • 递归镜像目录。
  • 在大多数类UNIX操作系统以及Windows上运行。
  • 无人值守/后台操作。
  • 支持持久的HTTP连接。
  • 支持使用OpenSSL或GnuTLS库进行加密下载的SSL/TLS。
  • 支持IPv4和IPv6下载。

在本文中,小编将简单介绍下wget实用程序,该实用程序通过HTTP、HTTPS、FTP和FTPS等广泛使用的协议从万维网(WWW)检索文件。

Wget下载

Wget的基本语法是:

$ wget [option] [URL]

首先,使用以下命令检查Linux系统中是否已经安装了wget实用程序

$ rpm -q wget         [RHEL/CentOS/FedoraRocky Linux/AlmaLinux系统]
$ dpkg -l | grep wget [Debian, Ubuntu and Mint系统]

在Linux系统中安装Wget

如果未安装Wget,可以使用Linux系统的默认包管理器安装它,如下命令:

$ sudo apt install wget -y      [Debian, Ubuntu和Mint系统]
$ sudo yum install wget -y      [RHEL/CentOS/FedoraRocky Linux/AlmaLinux系统]
$ sudo emerge -a net-misc/wget  [Gentoo Linux系统]
$ sudo pacman -Sy wget          [Arch Linux系统]
$ sudo zypper install wget      [OpenSUSE系统]

注意:此处使用的-y选项是在安装任何软件包之前阻止确认提示。

以下将简单分享整理10个Wget命令示例。

1、使用Wget下载文件

该命令将下载单个文件并将其存储在当前目录中,它还显示下载进度、大小、日期和下载时间。

# wget http://ftp.gnu.org/gnu/wget/wget2-2.0.0.tar.gz

--2022-01-26 09:15:16--  http://ftp.gnu.org/gnu/wget/wget2-2.0.0.tar.gz
Resolving ftp.gnu.org (ftp.gnu.org)... 209.51.188.20, 2001:470:142:3::b
Connecting to ftp.gnu.org (ftp.gnu.org)|209.51.188.20|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3565643 (3.4M) [application/x-gzip]
Saving to: ‘wget2-2.0.0.tar.gz’

wget2-2.0.0.tar.gz      100%[==========>]   3.40M  2.31MB/s    in 1.5s    

2022-01-26 09:15:16 (2.31 MB/s) - ‘wget2-2.0.0.tar.gz’ saved [3565643/3565643]

2、Wget下载不同名称的文件

使用-O大写)选项,下载文件以不同的文件名保存。在这里,小编命名的是wget.zip文件名,如下所示。

# wget -O wget.zip http://ftp.gnu.org/gnu/wget/wget2-2.0.0.tar.gz

--2022-01-26 09:15:18--  http://ftp.gnu.org/gnu/wget/wget-1.5.3.tar.gz
Resolving ftp.gnu.org (ftp.gnu.org)... 209.51.188.20, 2001:470:142:3::b
Connecting to ftp.gnu.org (ftp.gnu.org)|209.51.188.20|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 446966 (436K) [application/x-gzip]
Saving to: ‘wget.zip’

wget.zip      100%[===================>] 436.49K   510KB/s    in 0.9s    

2022-01-26 09:15:18 (510 KB/s) - ‘wget.zip’ saved [446966/446966]

3、wget使用HTTP和FTP协议下载多个文件

在这里,小编将使用HTTP和FTP协议使用wget命令一次性下载多个文件。

$ wget http://ftp.gnu.org/gnu/wget/wget2-2.0.0.tar.gz ftp://ftp.gnu.org/gnu/wget/wget2-2.0.0.tar.gz.sig

--2022-01-26 09:19:18--  http://ftp.gnu.org/gnu/wget/wget2-2.0.0.tar.gz
Resolving ftp.gnu.org (ftp.gnu.org)... 209.51.188.20, 2001:470:142:3::b
Connecting to ftp.gnu.org (ftp.gnu.org)|209.51.188.20|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3565643 (3.4M) [application/x-gzip]
Saving to: ‘wget2-2.0.0.tar.gz’

wget2-2.0.0.tar.gz      100%[==========>]   4.40M  4.31MB/s    in 1.1s    

2022-01-26 09:19:18 (2.31 MB/s) - ‘wget2-2.0.0.tar.gz’ saved [3565643/3565643]

4、wget同时下载多个文件

要一次下载多个文件,请使用-i包含要下载的URL列表的文件位置的选项。如下所示,每个URL都需要添加到单独的行中。

例如,以下载“ download-linux.txt ”文件,文件包含要下载的URL 表。

# cat download-linux.txt 

https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-desktop-amd64.iso
https://download.rockylinux.org/pub/rocky/8/isos/x86_64/Rocky-8.5-x86_64-dvd1.iso
https://cdimage.debian.org/debian-cd/current/amd64/iso-dvd/debian-11.2.0-amd64-DVD-1.iso
# wget -i download-linux.txt

--2022-01-26 09:29:18--  https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-desktop-amd64.iso
Resolving releases.ubuntu.com (releases.ubuntu.com)... 91.189.88.248, 91.189.88.247, 91.189.91.124, ...
Connecting to releases.ubuntu.com (releases.ubuntu.com)|91.189.88.248|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3071934464 (2.9G) [application/x-iso9660-image]
Saving to: ‘ubuntu-20.04.3-desktop-amd64.iso’

ubuntu-20.04.3-desktop-amd64   4%[=>      ] 137.71M  11.2MB/s    eta 3m 30s
...

如果你的URL列表具有特定的编号模式,可以添加花括号来获取与该模式匹配的所有URL。例如,如果要下载从5.1.1到 5.1.15的一系列 Linux 内核,可以执行以下操作。

$ wget https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.1.{1..15}.tar.gz

--2022-01-26 09:29:27--  https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.1.1.tar.gz
Resolving mirrors.edge.kernel.org (mirrors.edge.kernel.org)... 147.75.95.133, 2604:1380:3000:1500::1
Connecting to mirrors.edge.kernel.org (mirrors.edge.kernel.org)|147.75.95.133|:443... connected.
WARNING: The certificate of ‘mirrors.edge.kernel.org’ is not trusted.
WARNING: The certificate of ‘mirrors.edge.kernel.org’ is not yet activated.
The certificate has not yet been activated
HTTP request sent, awaiting response... 200 OK
Length: 164113671 (157M) [application/x-gzip]
Saving to: ‘linux-5.1.1.tar.gz’

linux-5.1.1.tar.gz      100%[===========>] 156.51M  2.59MB/s    in 61s     

2022-01-26 09:29:44 (2.57 MB/s) - ‘linux-5.1.1.tar.gz’ saved [164113671/164113671]

--2022-01-26 09:29:44--  https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.1.2.tar.gz
Reusing existing connection to mirrors.edge.kernel.org:443.
HTTP request sent, awaiting response... 200 OK
Length: 164110470 (157M) [application/x-gzip]
Saving to: ‘linux-5.1.2.tar.gz’

linux-5.1.2.tar.gz     19%[===========]  30.57M  2.58MB/s    eta 50s

5、wget恢复未完成下载

在大文件下载的情况下,有时可能会停止下载,在这种情况下可以通过-c选项继续下载,以便停止下载的相同文件。

否则,当开始下载文件而不指定-c选项时, wget将在文件末尾添加.1扩展名,被视为全新下载。因此,在下载大文件时添加-c开关是一种很好的做法。

# wget -c https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-desktop-amd64.iso

--2022-01-26 09:33:43--  https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-desktop-amd64.iso
Resolving releases.ubuntu.com (releases.ubuntu.com)... 91.189.88.247, 91.189.91.123, 91.189.91.124, ...
Connecting to releases.ubuntu.com (releases.ubuntu.com)|91.189.88.247|:443... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 3071934464 (2.9G), 2922987520 (2.7G) remaining [application/x-iso9660-image]
Saving to: ‘ubuntu-20.04.3-desktop-amd64.iso’

ubuntu-20.04.3-desktop-amd64.iso        5%[++++++> ]   167.93M  11.1MB/s               
^C
[root@tecmint ~]# wget -c https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-desktop-amd64.iso
--2022-01-26 09:33:47--  https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-desktop-amd64.iso
Resolving releases.ubuntu.com (releases.ubuntu.com)... 91.189.88.248, 91.189.91.124, 91.189.91.123, ...
Connecting to releases.ubuntu.com (releases.ubuntu.com)|91.189.88.248|:443... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 3071934464 (2.9G), 2894266368 (2.7G) remaining [application/x-iso9660-image]
Saving to: ‘ubuntu-20.04.3-desktop-amd64.iso’

ubuntu-20.04.3-desktop-amd64.iso        10%[+++++++=====> ] 296.32M  17.2MB/s    eta 2m 49s ^

6、Wget镜像整个网站

要下载或镜像或复制整个网站以供离线查看,可以使用以下命令制作网站的本地副本以及所有数据(JavaScript、CSS、图像等)。

$ wget --recursive --page-requisites --adjust-extension --span-hosts --convert-links --restrict-file-names=windows --domains yoursite.com --no-parent yoursite.com

上述命令的详细解释。

wget\
     --recursive \ # 下载整个站点。
     --page-requisites \ # 获取所有资产/元素(CSS/JS/图像)。
     --adjust-extension \ # 以.html结尾的文件保存。
     --span-hosts \ # 也包括来自异地的必要资产。
     --convert-links \ # 更新链接以在静态版本中仍然有效。
     --restrict-file-names=windows \ # 修改文件名以在Windows中也能正常工作。
     --domains yoursite.com \ # 不要跟踪此域名之外的链接。
     --no-parent \ # 不要跟踪你传入的目录之外的链接。
         yoursite.com/whatever/path # 要下载的URL地址

7、wget后台下载文件

使用-b选项,可以在下载开始后立即在后台发送下载,并将日志写入wget.log文件。

$ wget -b wget.log https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-desktop-amd64.iso

Continuing in background, pid 8999.
Output will be written to ‘wget.log’.

8、wget设置文件下载速度限制

使用选项--limit-rate=100k,下载速度限制为100k,限速情况将在wget.log下创建,如下所示。

$ wget -c --limit-rate=100k -b wget.log https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-desktop-amd64.iso

Continuing in background, pid 9108.
Output will be written to ‘wget-log’.

查看wget.log文件并检查wget的下载速度。

$ tail -f wget-log 

 5600K .......... .......... .......... .......... ..........  0%  104K 8h19m
 5650K .......... .......... .......... .......... ..........  0%  103K 8h19m
 5700K .......... .......... .......... .......... ..........  0%  105K 8h19m
 5750K .......... .......... .......... .......... ..........  0%  104K 8h18m
 5800K .......... .......... .......... .......... ..........  0%  104K 8h18m
 5850K .......... .......... .......... .......... ..........  0%  105K 8h18m
 5900K .......... .......... .......... .......... ..........  0%  103K 8h18m
 5950K .......... .......... .......... .......... ..........  0%  105K 8h18m
 6000K .......... .......... .......... .......... ..........  0% 69.0K 8h20m
 6050K .......... .......... .......... .......... ..........  0%  106K 8h19m
 6100K .......... .......... .......... .......... ..........  0% 98.5K 8h20m
 6150K .......... .......... .......... .......... ..........  0%  110K 8h19m
 6200K .......... .......... .......... .......... ..........  0%  104K 8h19m
 6250K .......... .......... .......... .......... ..........  0%  104K 8h19m
...

9、Wget通过FTP和HTTP下载受密码保护的文件

要从受密码保护的FTP服务器下载文件,可以使用选项--ftp-user=username--ftp-password=password如下命令:

$ wget --ftp-user=narad --ftp-password=password ftp://ftp.example.com/filename.tar.gz

要从受密码保护的HTTP服务器下载文件,可以使用选项--http-user=username--http-password=password如下命令:

$ wget --http-user=narad --http-password=password http://http.example.com/filename.tar.gz

10、Wget忽略SSL证书检查

要在通过HTTPS下载文件时忽略SSL证书检查,可以使用以下选项:--no-check-certificate

$ wget --no-check-certificate https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.1.1.tar.gz

--2021-01-26 09:51:21--  https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.1.1.tar.gz
Resolving mirrors.edge.kernel.org (mirrors.edge.kernel.org)... 147.75.95.133, 2604:1380:3000:1500::1
Connecting to mirrors.edge.kernel.org (mirrors.edge.kernel.org)|147.75.95.133|:443... connected.
WARNING: The certificate of ‘mirrors.edge.kernel.org’ is not trusted.
WARNING: The certificate of ‘mirrors.edge.kernel.org’ is not yet activated.
The certificate has not yet been activated
HTTP request sent, awaiting response... 200 OK
Length: 164113671 (157M) [application/x-gzip]
Saving to: ‘linux-5.1.1.tar.gz’
...

11、Wget帮助页面

使用选项--version--help,可以根据自己的需要查看版本和帮助。

$ wget --version
$ wget --help

总结

在本文中,小编简单的介绍了Linux wget命令以及用于日常管理任务的使用示例,仅供参考。如果想了解更多信息,请执行man wget命令查看更多。

未经允许不得转载:惠主机 » 分享整理Linux系统中10个Wget(Linux文件下载器)命令示例

相关文章