网站搜索

Linux 中的 10 个 Wget(Linux 文件下载器)命令示例


在本文中,我们将回顾 wget 实用程序,它使用广泛使用的协议(例如 HTTP)从万维网 (WWW) 检索文件HTTPSFTPFTPS

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

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

Wget 命令语法

Wget的基本语法是:

wget [option] [URL]

首先,使用以下命令检查您的 Linux 机器中是否已安装 wget 实用程序。

rpm -q wget         [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux]
dpkg -l | grep wget [On Debian, Ubuntu and Mint]

在 Linux 上安装 Wget

如果未安装 Wget,您可以使用 Linux 系统的默认包管理器进行安装,如图所示。

sudo apt install wget -y      [On Debian, Ubuntu and Mint]
sudo yum install wget -y      [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux]
sudo emerge -a net-misc/wget  [On Gentoo Linux]
sudo pacman -Sy wget           [On Arch Linux]
sudo zypper install wget      [On OpenSUSE]    

这里使用的 -y 选项是为了防止在安装任何软件包之前出现确认提示。有关更多 YUMAPT 命令示例和选项,请阅读我们的文章:

  • 20 个用于包管理的 Linux YUM 命令
  • Ubuntu/Debian 和 Mint 中的 15 个 APT 命令示例
  • 管理 OpenSUSE Linux 的 45 个 Zypper 命令示例

1.使用Wget下载文件

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

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

--2021-12-10 04: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    

2021-12-10 04:15:18 (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

--2021-12-10 04:20:19--  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    

2021-12-10 04:20:21 (510 KB/s) - ‘wget.zip’ saved [446966/446966]

3. Wget使用HTTP和FTP协议下载多个文件

在这里,我们了解如何使用 wget 命令同时使用 HTTPFTP 协议下载多个文件。

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

--2021-12-10 06:45:17--  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    

2021-12-10 06:46:10 (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

--2021-12-10 04:52:40--  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

--2021-12-10 05:46:59--  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     

2021-12-10 05:48:01 (2.57 MB/s) - ‘linux-5.1.1.tar.gz’ saved [164113671/164113671]

--2021-12-10 05:48:01--  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

--2021-12-10 05:27:59--  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
--2021-12-10 05:28:03--  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 \ # Download the whole site.
     --page-requisites \ # Get all assets/elements (CSS/JS/images).
     --adjust-extension \ # Save files with .html on the end.
     --span-hosts \ # Include necessary assets from offsite as well.
     --convert-links \ # Update links to still work in the static version.
     --restrict-file-names=windows \ # Modify filenames to work in Windows as well.
     --domains yoursite.com \ # Do not follow links outside this domain.
     --no-parent \ # Don't follow links outside the directory you pass in.
         yoursite.com/whatever/path # The URL to download

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-12-10 06:21: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?请通过我们的评论框分享,或者如果我们错过了任何内容,请告诉我们。