Linux運維基礎之wget命令詳解

Linux運維基礎之wget命令詳解

curl 命令不同,wget 通常用於將整個網站或特定檔案從伺服器下載到使用者的本地系統,而且預設情況下是遞迴下載。這意味著它可以瀏覽網站,跟隨連結下載整個網站,包括文字和媒體檔案。

下面是一些使用 wget 命令的不同方法:

基本用法

使用 wget 最簡單的方法是輸入 wget,然後輸入要下載檔案的 URL。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
wget http://example.com/file.zip
wget http://example.com/file.zip
wget http://example.com/file.zip

1. 指定下載目錄

如果要將檔案下載到特定目錄,可以使用 -P 選項,後面跟上目錄路徑。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
wget -P /path/to/directory http://example.com/file.zip
wget -P /path/to/directory http://example.com/file.zip
wget -P /path/to/directory http://example.com/file.zip

2. 在後臺下載

如果要下載大檔案,可能需要將下載轉移到後臺。可以使用 -b 選項來實現。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
wget -b http://example.com/large-file.zip
wget -b http://example.com/large-file.zip
wget -b http://example.com/large-file.zip

3. 恢復中斷的下載

如果下載被中斷,可以使用 -c 選項繼續下載。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
wget -c http://example.com/large-file.zip
wget -c http://example.com/large-file.zip
wget -c http://example.com/large-file.zip

4. 限制下載速度

如果不想讓 wget 佔用所有可用的網路頻寬,可以使用 --limit-rate 選項來限制下載速度。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
wget --limit-rate=200k http://example.com/file.zip
wget --limit-rate=200k http://example.com/file.zip
wget --limit-rate=200k http://example.com/file.zip

5. 下載多個檔案

如果要下載多個檔案,可以一次性指定所有檔案。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
wget http://example.com/file1.zip http://example.com/file2.zip
wget http://example.com/file1.zip http://example.com/file2.zip
wget http://example.com/file1.zip http://example.com/file2.zip

或者,也可以將所有 URL 放入一個檔案(每行一個 URL),然後使用 -i 選項。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
wget -i urls.txt
wget -i urls.txt
wget -i urls.txt

6. 下載完整網站

如果要下載完整的網站以供離線檢視,可以使用 -r (或 --recursive )選項。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
wget --recursive --no-clobber --page-requisites --html-extension --convert-links --restrict-file-names=windows --domains example.com --no-parent http://www.example.com
wget --recursive --no-clobber --page-requisites --html-extension --convert-links --restrict-file-names=windows --domains example.com --no-parent http://www.example.com
wget --recursive --no-clobber --page-requisites --html-extension --convert-links --restrict-file-names=windows --domains example.com --no-parent http://www.example.com

該命令將下載整個 www.example.com 網站。 該命令中使用的選項有以下作用:

  • --recursive:下載整個網站。
  • --domains example.com:不跟蹤 example.com 以外的連結。
  • --no-parent:不跟蹤 tutorials/html/ 目錄之外的連結。
  • --page-requisites:獲取構成頁面的所有元素(影象、CSS 等)。
  • --html-extension:使用 .html 副檔名儲存檔案。
  • --convert-links:轉換連結,使其在本地離線執行。
  • --restrict-file-names=windows:修改檔名,使其也能在 Windows 下執行。
  • --no-clobber:不覆蓋任何現有檔案(用於下載中斷和恢復的情況)。

7. 從 FTP 伺服器下載檔案

你可以使用 wget 從 FTP 伺服器下載檔案。如果需要使用者名稱和密碼,請使用以下格式: ftp://user:password@server/path

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
wget ftp://user:password@ftp.example.com/file.zip
wget ftp://user:password@ftp.example.com/file.zip
wget ftp://user:password@ftp.example.com/file.zip

更多 Linux 命令

目錄操作 rmdir · cd · pwd · exa · ls
檔案操作 cat · cp · dd · less · touch · ln · rename · more · head
檔案系統操作 chown · mkfs · locate
網路 ping · curl · wget · iptables · mtr
搜尋和文字處理 find · grep · sed · whatis · ripgrep · fd · tldr
系統資訊和管理 env · history · top · who · htop · glances · lsof
使用者和會話管理 screen · su · sudo · open

評論留言