如何使用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.tw/file.zip --2023-02-24 11:25:45-- https://tutorialspoint.tw/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.tw/file1.zip https://tutorialspoint.tw/file2.zip https://tutorialspoint.tw/file3.zip
在此示例中,get 命令從 tutorialspoint.com 網站下載三個檔案,並將它們儲存到 /home/user/Downloads 目錄。輸出將如下所示,包含與三個檔案相關的所有資訊。
輸出
--2023-03-03 10:22:38-- https://tutorialspoint.tw/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-- https://tutorialspoint.tw/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-- https://tutorialspoint.tw/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.tw/myfile.zip
輸出命令:−
--2023-02-24 14:30:00-- https://tutorialspoint.tw/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 都是一項寶貴的技能。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP