Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

Linux wget 指令實戰大全:11 大核心場景從單檔下載到鏡像整站

編輯:關於 Linux

wget 是 Linux 非互動式下載利器,支援 HTTP/HTTPS/FTP、斷點續傳、背景下載、限速、遞歸鏡像、密碼認證等豐富功能。本文整理 11 大實戰場景,附指令範例與參數解析,涵蓋從簡單下載到企業級部署自動化的完整技能樹。

場景 1:單檔下載 (基礎)

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

預設儲存於當前目錄,檔名取自 URL 最後段。

場景 2:斷點續傳 (大檔必備)

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

-c, --continue:網路中斷後重新執行同指令,自動從已下載位置續傳。

場景 3:背景下載 (釋放終端機)

wget -b https://mirror.example.com/CentOS-7-x86_64-DVD.iso
# 輸出:Continuing in background, pid 12345.
# Output will be written to 'wget-log'.

進程轉背景執行,日誌寫入 wget-log。可用 tail -f wget-log 監控進度。

場景 4:限制下載速率 (共享頻寬)

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

限制 100 KB/s,避免佔滿頻寬影響其他服務。單位:k=KB, m=MB。

場景 5:批次下載多個 URL (清單檔)

# 建立清單
cat > download-list.txt << EOF
https://example.com/file1.iso
https://example.com/file2.tar.gz
https://example.com/file3.zip
EOF

# 批次下載
wget -i download-list.txt

場景 6:增加重試次數 (不穩定網路)

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

預設重試 20 次。不穩定網路或大檔下載建議設定 50-100 次。

場景 7:日誌重導向 (自動化腳本)

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

所有輸出寫入 download.log,適合 cron 定時任務記錄。

場景 8:鏡像整站 (離線瀏覽/備份)

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

參數組合解析:

  • --mirror:等同 -r -N -l inf --no-remove-listing (遞歸、時間戳、無限深度)
  • -p:下載頁面完整呈現所需資源 (CSS、JS、圖片)
  • --convert-links:下載完成後轉換連結為相對路徑,支援離線瀏覽
  • -P ./local-copy:指定儲存根目錄

場景 9:拒絕特定檔案類型 (節省空間)

wget --reject=png,jpg,gif,mp4 --mirror https://example.com

不下載圖片、影片等大型媒體檔,僅保留 HTML/文字內容。

場景 10:設定下載配額 (總量控制)

wget -Q10m -i download-list.txt

總下載量超過 10MB 自動停止。注意:單一檔案不受配額限制,僅對清單/遞歸下載生效。

場景 11:認證下載 (密碼保護資源)

# FTP 認證
wget --ftp-user=username --ftp-password=password ftp://server/file.iso

# HTTP Basic Auth (URL 嵌入帳密,不安全)
wget https://user:pass@server/file.iso

# 較安全:使用 .netrc 或 .wgetrc
echo "machine server login user password pass" > ~/.netrc
chmod 600 ~/.netrc
wget https://server/file.iso

進階組合範例:企業級備份腳本

#!/bin/bash
# 每日備份遠端目錄,限速 5MB/s,斷點續傳,日誌記錄
wget -c --limit-rate=5m --tries=50 -o /var/log/backup_wget.log \
     --mirror -p --convert-links -P /backup/remote_mirror \
     --reject="*.log,*.tmp" \
     https://internal-repo.company.com/releases/

wget vs curl 選擇指南

場景推薦工具
非互動式大檔/批次/鏡像下載wget
API 呼叫、JSON 處理、複雜 Header、POST 資料curl
斷點續傳穩定性wget (略優)
協定支援廣度curl (支援更多協定)

總結

掌握 -c (續傳)、-b (背景)、--limit-rate (限速)、-i (清單)、--mirror (鏡像)、--reject (過濾)、-Q (配額) 七大核心參數,即可應對 95% 下載自動化需求。配合 .netrc 管理認證,構建安全、可維運的下載自動化流程。

Copyright © Windows教程網 All Rights Reserved