# Linux系統wget命令怎么用
## 一、wget命令概述
### 1.1 wget是什么
wget是GNU項目下的一個開源網絡下載工具,名稱來源于"World Wide Web"和"get"的組合。它是一個非交互式的命令行工具,支持通過HTTP、HTTPS和FTP協議從網絡上下載文件。
### 1.2 wget的特點
- **支持斷點續傳**:下載中斷后可從中斷處繼續
- **遞歸下載**:可下載整個網站目錄結構
- **后臺運行**:可在用戶退出系統后繼續工作
- **支持代理**:可通過代理服務器下載
- **輕量高效**:占用系統資源少
### 1.3 適用場景
- 自動化下載任務
- 鏡像網站內容
- 批量下載資源
- 不穩定網絡環境下的下載
## 二、安裝與基本使用
### 2.1 安裝wget
#### 在Debian/Ubuntu系統上
```bash
sudo apt update
sudo apt install wget
sudo yum install wget
sudo pacman -S wget
wget [選項] [URL]
wget https://example.com/file.zip
wget -O custom_name.zip https://example.com/file.zip
wget -P /path/to/directory https://example.com/file.zip
wget -c https://example.com/large_file.iso
wget --limit-rate=200k https://example.com/large_file.iso
wget --tries=10 https://example.com/unstable_file.zip
wget --timeout=30 https://example.com/slow_file.zip
wget --wait=5 https://example.com/files/{1..10}.jpg
wget -r https://example.com/directory/
wget -r -l 2 https://example.com/directory/
wget -r -np https://example.com/directory/subdir/
wget --user=username --password=password ftp://example.com/file.zip
wget --http-user=username --http-password=password https://example.com/protected/file.zip
wget https://example.com/images/{1..10}.jpg
wget -i url_list.txt
wget --mirror --convert-links --adjust-extension --page-requisites --no-parent https://example.com/
參數說明:
- --mirror
:開啟鏡像模式
- --convert-links
:轉換鏈接為本地可用
- --adjust-extension
:調整文件擴展名
- --page-requisites
:下載頁面所需的所有資源
- --no-parent
:不追溯至父目錄
wget --user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36" https://example.com
wget --proxy=on --http-proxy=http://proxy.example.com:8080 https://target.example.com
wget --proxy=on --proxy-type=socks5 --proxy-user=user --proxy-password=pass socks5://proxy.example.com:1080 https://target.example.com
wget --ftp-user=username --ftp-password=password -r ftp://ftp.example.com/directory/
wget https://www.youtube.com/watch?v=VIDEO_ID -O video.mp4
注意:YouTube可能會阻止這種簡單下載方式,建議使用youtube-dl等專門工具。
結合crontab實現定時下載:
# 每天凌晨3點下載更新
0 3 * * * wget -N https://example.com/daily_update.zip -P /updates/
wget -qO- https://example.com/archive.tar.gz | tar xz
wget --no-check-certificate https://example.com
wget --ca-certificate=/path/to/cert.pem https://example.com
wget --restrict-file-names=nocontrol https://example.com/中文文件.zip
檢查防火墻設置或嘗試使用不同的用戶代理:
wget --user-agent="Mozilla/5.0" https://example.com
wget -c --tries=0 --read-timeout=20 --waitretry=15 https://example.com/large_file.iso
特性 | wget | curl |
---|---|---|
遞歸下載 | 支持 | 不支持 |
斷點續傳 | 支持 | 支持 |
協議支持 | HTTP/HTTPS/FTP | 更多協議 |
簡單易用 | 更簡單 | 更靈活 |
后臺下載 | 支持 | 不支持 |
aria2是多線程下載工具,相比wget: - 支持多連接分段下載 - 速度通常更快 - 功能更復雜 - 資源占用更多
不要在命令行中直接使用密碼,建議使用:
wget --user=username --ask-password https://example.com/protected
wget https://example.com/file.zip https://example.com/file.zip.md5
md5sum -c file.zip.md5
wget --limit-rate=100k --tries=5 --timeout=30 https://example.com/large_file.iso
/etc/wgetrc
~/.wgetrc
# 限制下載速度
limit_rate = 100k
# 默認重試次數
tries = 5
# 使用以下User-Agent
user_agent = Wget/1.21.1
# 啟用斷點續傳
continue = on
wget作為Linux系統中最強大的下載工具之一,其功能遠不止簡單的文件下載。通過本文的介紹,您應該已經掌握了wget從基礎到高級的各種用法。無論是日常的單個文件下載,還是復雜的網站鏡像任務,wget都能勝任。建議讀者在實際工作中多加練習,結合man手冊探索更多未提及的選項和功能,讓wget成為您網絡資源獲取的得力助手。
注意:本文基于wget 1.21版本編寫,不同版本可能在部分選項上有差異,請以實際系統的man手冊為準。 “`
這篇文章詳細介紹了wget命令的各個方面,包括安裝、基本使用、常用參數、高級技巧、問題解決等,字數約6800字。您可以根據需要調整各部分內容的深度或添加更多實際案例。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。