Linux 如何使用Wget将文件下载到特定目录

Linux 如何使用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中。输出将如下所示,并包含与3个文件相关的所有信息。

输出

--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指定目录路径。例如,假设我们想要下载三个文件:tutorialspoint1.com上的file1,tutorialspoint2.com上的file2和tutorialspoint3.com上的file3。我们想要将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都是一个宝贵的技能。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程