別再傻傻分不清!curl 和 wget 的正確使用姿勢,90% 的人都搞混了
今天分享一下curl和wget這兩個比較常用的命令行工具,但很多人在使用時會混淆它們的定位和適用場景。

1. 區(qū)別:定位不同
wget:專注于下載,curl:專注于數(shù)據(jù)傳輸
簡單來說,如果你想下載文件,用wget;如果你想調(diào)試API、傳輸復(fù)雜數(shù)據(jù),用curl。
2. wget:專業(yè)的下載工具
(1) 核心用途
- 遞歸下載整個網(wǎng)站
- 斷點續(xù)傳大文件
- 批量下載資源
(2) 常用操作
# 基本下載,后面直接加URL
wget https://packages.gitlab.com/gitlab/gitlab-ce/packages/ol/9/gitlab-ce-18.2.8-ce.0.el9.x86_64.rpm
# -c: 斷點續(xù)傳(生產(chǎn)環(huán)境必備!)
wget -c https://packages.gitlab.com/gitlab/gitlab-ce/packages/ol/9/gitlab-ce-18.2.8-ce.0.el9.x86_64.rpm
# -b:后臺下載
wget -b https://packages.gitlab.com/gitlab/gitlab-ce/packages/ol/9/gitlab-ce-18.2.8-ce.0.el9.x86_64.rpm
# 限速下載(避免影響業(yè)務(wù))
wget --limit-rate=200k https://packages.gitlab.com/gitlab/gitlab-ce/packages/ol/9/gitlab-ce-18.2.8-ce.0.el9.x86_64.rpm
# -r:遞歸下載整個目錄
wget -r -np -nH https://baidu.com/files/
# 下載并重命名
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
# 指定重試次數(shù)
wget -t 5 https://baidu.com/unstable-file.zip
# 超時設(shè)置
wget -T 30 https://baidu.com/slow-file.zip
3. curl:強(qiáng)大的數(shù)據(jù)傳輸工具
(1) 核心用途
- API調(diào)試和測試
- 復(fù)雜HTTP請求
- 文件上傳
- 協(xié)議支持廣泛(支持20+種協(xié)議)
(2) 常用操作
# 基本請求
curl curl https://ligelinux.com
# 顯示詳細(xì)請求信息(調(diào)試神器)
curl -v https://ligelinux.com/console
# 只顯示響應(yīng)頭
curl -I https://ligelinux.com/console
# 指定請求方法
curl -X POST https://ligelinux.com/console
curl -X GET https://ligelinux.com/console
# 發(fā)送JSON數(shù)據(jù)
curl -X POST \
-H "Content-Type: application/json" \
-d '{"name":"張三","email":"zhangsan@example.com"}' \
https://baidu.com/users
# 輸出到文件
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
# 跟隨重定向
curl -L https://ligelinux.com/console
4. 關(guān)鍵區(qū)別總結(jié)
特性 | wget | curl |
主要定位 | 下載工具 | 數(shù)據(jù)傳輸工具 |
協(xié)議支持 | HTTP, HTTPS, FTP | 20+種協(xié)議 |
遞歸下載 | 原生支持 | 不支持 |
斷點續(xù)傳 | 原生支持 | 需要特定參數(shù) |
API調(diào)試 | 有限 | 非常強(qiáng)大 |
輸出默認(rèn) | 保存到文件 | 輸出到stdout |
5. 常見誤區(qū)糾正
(1) 誤區(qū)1:curl不能下載文件
錯誤:很多人認(rèn)為curl只能查看內(nèi)容不能下載
正確:curl也能下載
# curl也可以下載文件
curl -O https://mirrors.aliyun.com/repo/Centos-7.repo # 保持原文件名
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
# 自定義文件名
(2) 誤區(qū)2:wget可以很好地進(jìn)行API調(diào)試
錯誤:用wget測試復(fù)雜API
正確:wget適合簡單下載,復(fù)雜API應(yīng)該用curl
(3) 誤區(qū)3:兩者可以完全互換
錯誤:認(rèn)為curl和wget功能相同
正確:各有專長,根據(jù)場景選擇
總之記住這個簡單的原則:下載用wget,調(diào)試用curl。



























