萬盛學電腦網

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

wget 命令大全:從基礎下載到遞歸鏡像、斷點續傳、代理與批量自動化

wget 核心優勢

  • 非互動/後台下載:啟動後可斷開 SSH,任務持續運行 (-b 後台)。
  • 斷點續傳:-c 斷網/重啟後接續下載,大檔案救星。
  • 遞歸鏡像:-r -k -p 完整鏡像網站,離線瀏覽。
  • 穩健重試:-t 0 無限重試,-w 間隔,適應惡劣網絡。
  • 腳本自動化友好:輸出可解析、支援輸入文件列表、代理配置靈活。

常用參數速查表

參數說明典型組合
-c, --continue斷點續傳大檔案必加
-t, --tries=N重試次數 (0=無限)-t 0 -w 10
-w, --wait=SEC重試間隔秒數
-b, --background後台運行配合 -o log.txt
-o, --output-file=FILE日誌輸出文件-o wget.log
-q, --quiet靜默模式 (無輸出)腳本自動化
-nv僅錯誤/基本信息平衡靜默與可見性
-r, --recursive遞歸下載鏡像站點
-l, --level=N遞歸深度 (預設 5, 0=無限)-l 2
-k, --convert-links轉換鏈接為本地相對路徑離線瀏覽必加
-p, --page-requisites下載頁面依賴 (CSS/JS/圖片)配合 -k -r
-np, --no-parent不追溯父目錄限制範圍
-nd, --no-directories不建立目錄結構,所有檔案平鋪單目錄收集
-nH, --no-host-directories不建立主機名目錄
--cut-dirs=N忽略遠端 N 層目錄結構--cut-dirs=1
-A, --accept=LIST接受的檔案模式 (逗號分隔)-A "*.jpg,*.png,*.pdf"
-R, --reject=LIST拒絕的檔案模式-R "*.html,*.php"
--limit-rate=RATE限速 (如 200k, 2M)--limit-rate=1M
-i, --input-file=FILE從文件讀取 URL 列表批量下載
--user=USER --password=PASSHTTP/FTP 認證
--http-user/--http-password僅 HTTP 認證
--proxy=on/off啟用/禁用代理--proxy=off
--no-check-certificate忽略 SSL 證書驗證 (不安全)自簽證書測試環境

經典實戰場景

1. 單檔案穩健下載 (大檔、惡劣網絡)

wget -c -t 0 -w 10 --limit-rate=2M -o download.log "https://example.com/bigfile.iso" &
# -c 續傳, -t 0 無限重試, -w 10 間隔10秒, --limit-rate 限速, -o 記錄日誌, & 後台

2. 完整鏡像靜態網站 (離線瀏覽)

wget -r -k -p -np -l 3 -e robots=off --user-agent="Mozilla/5.0" "https://example.com/docs/"
# -r 遞歸, -k 轉鏈接, -p 頁面必需資源, -np 不上溯父目錄, -l 3 深度3層
# -e robots=off 忽略 robots.txt (僅限授權/自有站點), --user-agent 偽裝瀏覽器

3. 批量下載 URL 列表

# urls.txt 內容: 每行一個 URL
wget -i urls.txt -c -t 3 -w 5 -P /data/downloads -o batch.log
# -P 指定儲存目錄

4. 下載特定類型檔案 (如所有 PDF/圖片)

wget -r -l 2 -A "*.pdf,*.jpg,*.png" -R "*.html,*.php" -nd -P ./images "https://example.com/gallery/"

5. FTP 目錄下載 (含認證)

wget -r -nH --cut-dirs=2 -c ftp://user:pass@ftp.example.com/pub/data/
# -nH 不建立主機目錄, --cut-dirs=2 跳過 ftp://host/pub/data/ 前兩層

6. 使用代理下載

# 環境變量方式 (影響所有支援的程式)
export http_proxy=https://proxy:3128
export https_proxy=https://proxy:3128
wget https://example.com/file

# 單次命令指定 (wget 獨有)
wget --proxy=on --proxy-user=user --proxy-password=pass "https://example.com/file"

# 關閉代理
wget --proxy=off "https://example.com/file"

7. 鏡像整站並生成靜態副本 (生產級參數)

wget \
  --recursive \
  --level=inf \
  --page-requisites \
  --convert-links \
  --adjust-extension \
  --no-parent \
  --no-host-directories \
  --cut-dirs=1 \
  --span-hosts \
  --domains=example.com,cdn.example.com \
  --exclude-directories=/wp-admin,/wp-json \
  --reject="*.json,*.xml,*.txt" \
  --wait=1 \
  --random-wait \
  --user-agent="Mozilla/5.0 (compatible; SiteMirror/1.0)" \
  -o mirror.log \
  "https://example.com/"

進階:配置文件 ~/.wgetrc

# 全局預設配置
default_options = -c -t 3 -w 5 -nv
# 代理
http_proxy = https://proxy:3128
https_proxy = https://proxy:3128
use_proxy = on
# 限速
limit_rate = 5m
# 輸出
output_file = wget.log

對比:wget vs curl

特性wgetcurl
遞歸下載/鏡像✅ 原生強大❌ 需腳本配合
斷點續傳✅ -c 簡單✅ -C - 較複雜
協議支援HTTP/HTTPS/FTP✅ 極豐富 (SFTP, SMB, POP3, IMAP...)
HTTP/2, HTTP/3❌ 無✅ 原生支援
API 測試/開發❌ 弱✅ 極強 (Header, Data, Form, Auth, --libcurl)
管線/腳本整合良優 (stdin/stdout 設計)

選擇建議:下載檔案/鏡像站點/批量任務 → wget;API 調試/複雜 HTTP 交互/現代協議 → curl。

copyright © 萬盛學電腦網 all rights reserved