萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

Linux wget 下載神器 11 大實戰範例:斷點續傳、限速、後台、鏡像站、FTP、遞歸、配額、認證全攻略

wget 是 Linux 系統內建的非互動式網路下載工具,支援 HTTP/HTTPS/FTP、斷點續傳、遞歸鏡像、頻寬限制、後台執行等強大功能。本文整理 11 個實戰場景與參數詳解,涵蓋從單檔下載到整站鏡像的完整技巧。

一、核心參數速查

參數長參數說明
-c--continue斷點續傳
-b--background後台執行
--limit-rate限制下載速率(如 100k, 2M)
-i--input-file從檔案讀取 URL 列表
-t--tries重試次數(預設 20)
-o--output-file記錄輸出到檔案
-Q--quota總下載配額
--mirror鏡像模式(遞歸+時間戳+無限深度)
-p--page-requisites下載頁面完整資源(CSS/JS/圖片)
--convert-links轉換連結為本地相對路徑
-P--directory-prefix指定儲存目錄
--reject拒絕特定副檔名(如 png)
--user/--passwordFTP/HTTP 基本認證

二、11 大實戰範例

1. 下載單一檔案

$ wget https://mirror.example.com/CentOS-7-x86_64-DVD.iso

2. 斷點續傳大檔案

$ wget -c https://mirror.example.com/CentOS-7-x86_64-DVD.iso

斷點續傳進度顯示

3. 後台下載

$ wget -b https://mirror.example.com/CentOS-7-x86_64-DVD.iso
# 輸出寫入 wget-log,可用 tail -f wget-log 監看

4. 限制下載速率

$ wget --limit-rate=100k https://mirror.example.com/large.iso

5. 批次下載多個 URL

$ cat download-list.txt
url1
url2
url3
$ wget -i download-list.txt

6. 增加重試次數

$ wget --tries=75 https://mirror.example.com/large.iso

7. 記錄下載日誌

$ wget -o download.log https://mirror.example.com/large.iso

8. 鏡像整個網站供離線瀏覽

$ wget --mirror -p --convert-links -P ./local-copy https://example.com

9. 拒絕特定檔案類型

$ wget --reject=png https://example.com

10. 設定總下載配額

$ wget -Q10m -i download-list.txt
# 超過 10MB 停止(不影響單檔下載完成)

11. 從需認證的 FTP/HTTP 下載

$ wget --ftp-user=user --ftp-password=pass ftp://example.com/file.zip
# 或 URL 內嵌認證(不安全,會殘留於歷史)
$ wget ftp://user:pass@example.com/file.zip

三、安全性提醒

  • 密碼參數會出現在進程列表(ps aux),建議寫入 ~/.netrc 或 ~/.wgetrc 並 chmod 600
  • 生產腳本請使用 --ask-password 互動式輸入

四、進階技巧

  • 頻寬節流排程:cron 執行 wget --limit-rate=500k -i nightly.txt
  • 斷點續傳自動化:腳本檢測 wget 退出碼非 0 時自動 -c 重試
  • 鏡像站同步:wget -m -np -nH --cut-dirs=2 -R "index.html*" https://mirror.example.com/centos/
copyright © 萬盛學電腦網 all rights reserved