curl 是支援 HTTP/HTTPS/FTP/SFTP 等協定的命令列傳輸工具,參數豐富,適用於自動化腳本、API 除錯、檔案下載上傳、爬蟲開發。
| 參數 | 功能 |
|---|---|
-o file | 輸出到指定檔名 |
-O | 使用遠端檔名儲存 |
-C - | 斷點續傳 |
-H "Header: val" | 自訂請求標頭 |
-A "UA string" | 偽裝 User-Agent |
-e "referer" | 偽裝 Referer (防盜鏈) |
-b file / -c file | 讀取/儲存 Cookie |
-x host:port | 指定 HTTP Proxy |
-u user:pass | Basic Auth 認證 |
-T file | 上傳檔案 (PUT) |
-X METHOD | 指定 HTTP 方法 (POST/PUT/DELETE) |
-d "data" | POST 資料 |
-s | 靜默模式 (不顯示進度) |
-w "%{http_code}" | 自訂輸出格式 (如狀態碼) |
-# | 進度條顯示 |
# 1. 基本下載並顯示
curl https://example.com
# 2. 儲存網頁
curl -o page.html https://example.com
curl -O https://example.com/file.zip
# 3. 測試 HTTP 狀態碼 (腳本適用)
curl -o /dev/null -s -w "%{http_code}" https://example.com
# 4. 使用 Proxy
curl -x 192.168.1.100:8080 https://example.com
# 5. Cookie 操作
curl -c cookies.txt https://example.com/login -d "user=foo&pass=bar"
curl -b cookies.txt https://example.com/profile
# 6. 偽裝瀏覽器
curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" https://example.com
# 7. 偽裝 Referer (防盜鏈)
curl -e "https://example.com/" https://example.com/image.jpg
# 8. 批次下載 (花括號展開)
curl -O "https://example.com/img[1-5].jpg"
curl -o "#1_#2.jpg" "https://example.com/{a,b}/img[1-5].jpg" # 重命名避免覆蓋
# 9. POST JSON API
curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' https://api.example.com/endpoint
# 10. 上傳檔案
curl -T localfile.txt ftp://user:pass@ftp.example.com/remotefile.txt

進階技巧:--limit-rate 100k限速;--retry 3 --retry-delay 5失敗重試;--cert --key客戶端憑證認證;--config讀取參數檔簡化長指令;結合jq處理 JSON 回應:curl -s api | jq .data。
Linux 系統安裝與配置 DenyHosts 防暴力破解工具完整指南
Linux 掛載 ISO 檔案完整指南:mount 指令與圖形化工具 Furius ISO Mount
Linux RRDtool 入門實戰:建庫、更新資料、繪圖三步驟完整指令範例
Linux netstat 查看 80 埠連線數完整指令集:總連線、ESTABLISHED、IP 統計與 Apache 並發分析
Debian Wheezy (Debian 7) 桌面優化完整指南:觸控板、小紅點、啟動音、快捷鍵與常用軟體
CentOS 6.0 手动升级至 6.2:替换 163 源与 yum update 实战
Linux 使用 S3QL 建立加密雲端檔案系統 (Amazon S3 / Google Cloud / OpenStack) 完整部署指南