curl 是 Linux 中常用的 URL 資料傳輸工具,支援 HTTP、HTTPS、FTP 協定,可用於下載、上傳、模擬表單、登入、cookie 處理與效能檢測。本文整理常用參數與 -w 輸出變量。
抓取頁面:
curl -o home.html https://example.com
保留遠端檔名:
curl -O https://example.com/file.jpg
模擬登入並保存 cookie:
curl -c ./cookie.txt -F log=user -F pwd=pass https://example.com/login.php
使用 cookie:
curl -b ./cookie.txt https://example.com/admin
斷點續傳:
curl -C - -O https://example.com/file.jpg
分段下載:
curl -r 0-100 -o part1 https://example.com/file.jpg curl -r 100-200 -o part2 https://example.com/file.jpg curl -r 200- -o part3 https://example.com/file.jpg
FTP 下載:
curl -u user:pass -O ftp://example.com/file.css
FTP 上傳:
curl -T test.sql ftp://user:pass@example.com/path/
-w 可用來輸出傳輸統計資訊,常見變量包括:
使用方式:
curl -w "http_code=%{http_code} time_total=%{time_total}\n" -o /dev/null -s https://example.com透過 curl,開發者可以快速完成文件抓取、表單提交、API 測試與傳輸效能分析。