如何使用 Wget 将文件下载到特定目录?
作为命令行工具的频繁用户,我们经常需要从互联网下载文件。"Wget"是执行此任务的常用工具之一,它提供了一种从命令行下载文件的高效方法。但是,我们发现了解如何指定下载文件的保存目录非常重要。
在本文中,我们将分享使用"Wget"将文件下载到特定目录的经验。我们将探讨可用于指定下载目录的不同命令行选项和参数,并通过实际示例来说明它们的用法。读完本文后,您将掌握使用 Wget 轻松将文件下载到特定目录的知识。
Wget 的基本语法是−
$ wget [option] [URL]
我们假设您对命令行操作有基本的了解,并且已经在您的系统上安装了 Wget。如果您的 Ubuntu 或基于 Debian 的系统上尚未安装 Wget,我们可以通过执行以下命令来安装它。
$ sudo apt-get update $ sudo apt-get install wget
1.将文件下载到特定目录
我们将解释如何在不更改当前工作目录的情况下下载文件并将其保存到特定目录。要设置保存所有检索到的文件和子目录的目录前缀,我们可以使用 Wget 的"-P"或"--directory-prefix"选项。这可以轻松地将下载的文件整理到不同的文件夹中,而无需在下载后手动移动文件。
使用"-P"命令将文件下载到特定目录的语法
$ wget -P /path/to/directory URL
运行此命令,我们可以得到如下输出
$ wget -P /home/user/Downloads https://tutorialspoint.com/file.zip --2023-02-24 11:25:45-- https://tutorialspoint.com/file.zip Resolvingtutorialspoint.com(tutorialspoint.com)..93.184.216.34,2606:2800:220:1:248:1893:25c8:1946 Connecting to tutorialspoint.com (tutorialspoint.com)|93.184.216.34|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 4781945 (4.6M) [application/zip] Saving to: '/home/user/Downloads/file.zip' /home/user/Downloads/file.zip 100%[==============================================================================>] 4.56M 2.39MB/s in 1.9s 2023-02-24 11:25:48 (2.39 MB/s) - '/home/user/Downloads/file.zip' saved [4781945/4781945]
在这个独特的示例中,该命令从网站 tutorialspoint.com 下载一个示例文件,并将文件保存到特定目录 /home/user/Documents。终端输出显示下载进度,包括域名解析、服务器连接、HTTP 请求以及下载文件的大小和类型。
2. 将多个文件下载到特定目录
Wget 命令是一个功能强大的命令行工具,可用于从网站或服务器下载多个文件。要下载多个文件,我们只需将要下载的文件的 URL 作为参数写入命令即可。我们还可以使用 -P 选项指定下载文件的保存目录。
输入
$ wget -P /home/user/Downloads https://tutorialspoint.com/file1.zip https://tutorialspoint.com/file2.zip https://tutorialspoint.com/file3.zip
在本例中,get 命令从网站 tutorialspoint.com 下载三个文件,并将它们保存到目录 /home/user/Downloads。输出将如下所示,包含与这三个文件相关的所有信息。
输出
--2023-03-03 10:22:38-- http://tutorialspoint.com/file1.zip Resolving tutorialspoint.com (tutorialspoint.com)... 93.184.216.34 Connecting to tutorialspoint.com (tutorialspoint.com)|93.184.216.34|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 250 [application/zip] Saving to: '/home/user/Downloads/file1.zip' /home/user/Downloads/file1.zip 100%[=================================================================>] 250 --.-KB/s in 0s 2023-03-03 10:22:39 (13.4 MB/s) - '/home/user/Downloads/file1.zip' saved [250/250] --2023-03-03 10:22:39-- http://tutorialspoint.com/file2.zip Resolving tutorialspoint.com (tutorialspoint.com)... 93.184.216.34 Connecting to tutorialspoint.com (tutorialspoint.com)|93.184.216.34|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 250 [application/zip] Saving to: '/home/user/Downloads/file2.zip' /home/user/Downloads/file2.zip 100%[=================================================================>] 250 --.-KB/s in 0s 2023-03-03 10:22:39 (15.2 MB/s) - '/home/user/Downloads/file2.zip' saved [250/250] --2023-03-03 10:22:39-- http://tutorialspoint.com/file3.zip Resolving tutorialspoint.com (tutorialspoint.com)... 93.184.216.34 Connecting to tutorialspoint.com (tutorialspoint.com)|93.184.216.34|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 250 [application/zip] Saving to: '/home/user/Downloads/file3.zip' /home/user/Downloads/file3.zip 100%[=================================================================>] 250 --.-KB/s in 0s 2023-03-03 10:22:39 (19.4 MB/s) - '/home/user/Downloads/file3.zip' saved [250/250]
3. 从多个网站下载文件到不同目录
当我们需要从多个网站下载文件并将它们整理到不同的目录中时,我们会使用 Wget 命令。使用 Wget,我们可以轻松地从不同的网站下载文件并将它们保存到不同的目录中,而无需在下载后手动移动每个文件。
为此,我们提供一个 URL 列表作为命令的参数,然后使用 -P 选项指定每个 URL 的目录路径。例如,假设我们要下载三个文件:file1(来自 tutorialspoint1.com)、file2(来自 tutorialspoint2.com)和 file3(来自 tutorialspoint3.com)。我们希望将 file1 保存到 /home/user/tutorialspoint1,将 file2 保存到 /home/user/tutorialspoint2,将 file3 保存到 /home/user/tutorialspoint3。
为此,我们将使用以下命令 -
$ wget -P /path/to/directory1 URL1 -P /path/to/directory2 URL2 -P /path/to/directory3 URL3 ...
以下命令的输出 −
--2023-02-25 06:00:00-- URL1 Resolving URL1 (URL1)... connected. HTTP request sent, awaiting response... 200 OK Length: 12345 (12K) [text/plain] Saving to: '/path/to/directory1/file1.txt' /path/to/directory1/file1.txt 100%[======================================>] 12.05K --.-KB/s in 0s --2023-02-25 06:00:01-- URL2 Resolving URL2 (URL2)... connected. HTTP request sent, awaiting response... 200 OK Length: 67890 (66K) [text/plain] Saving to: '/path/to/directory2/file2.txt' /path/to/directory2/file2.txt 100%[======================================>] 66.28K --.-KB/s in 0s --2023-02-25 06:00:02-- URL3 Resolving URL3 (URL3)... connected. HTTP request sent, awaiting response... 200 OK Length: 13579 (13K) [text/plain] Saving to: '/path/to/directory3/file3.txt' /path/to/directory3/file3.txt 100%[======================================>] 13.26K --.-KB/s in 0s
使用 wget 下载大文件可能非常耗时,而网络错误则可能使其更加令人沮丧。wget 中的"-c 或 --continue"命令允许您继续下载未完成下载的文件。此功能在您需要暂停或重新开始下载时非常有用,可以节省您的时间和精力。
输入命令 −
$ wget -c https://tutorialspoint.com/myfile.zip
输出命令 −
--2023-02-24 14:30:00-- https://tutorialspoint.com/myfile.zip Resolving tutorialspoint.com (tutorialspoint.com)... 192.169.82.67 Connecting to tutorialspoint.com (tutorialspoint.com)|192.169.82.67|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 1024000 (1000K) [application/zip] Saving to: 'myfile.zip' myfile.zip 0%[ ] 0 --.-KB/s myfile.zip 25%[== ] 254.49K 1.09MB/s myfile.zip 50%[======== ] 512.00K 1.20MB/s myfile.zip 75%[==============> ] 768.00K 1.27MB/s myfile.zip 100%[===================>] 1000K 1.35MB/s in 0.8s 2023-02-24 14:30:01 (1.35 MB/s) - 'myfile.zip' saved [1024000/1024000]
有关 wget 命令的更多信息,我们可以编写 man wget 命令。
$ man wget
结论
总而言之,使用 wget 命令可以方便高效地将文件从互联网下载到系统上的特定目录。通过使用 -P 命令,您可以指定下载文件的保存目录。总而言之,掌握 wget 及其各种选项的使用方法可以显著简化和精简您的文件下载过程。无论您是经验丰富的 Linux 用户还是刚刚入门,了解如何使用 wget 都是您工具包中一项宝贵的技能。
相关文章
有用资源
linux 参考教程 - 该教程包含有关 linux 的更多信息:https://www.cainiaomax.com/linux/

