Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

Linux curl 命令詳解:常用參數與 -w 輸出變量

curl 是 Linux 中常用的 URL 資料傳輸工具,支援 HTTP、HTTPS、FTP 協定,可用於下載、上傳、模擬表單、登入、cookie 處理與效能檢測。本文整理常用參數與 -w 輸出變量。

常用參數

  • -o:把輸出寫入指定文件。
  • -O:保留遠端文件名稱。
  • -C -:斷點續傳。
  • -d:以 POST 方式傳送資料。
  • -F:模擬表單提交。
  • -b:使用 cookie 文件。
  • -c:將 cookie 寫入文件。
  • -D:把 header 寫入文件。
  • -e:設定來源網址。
  • -x:使用代理。
  • -r:指定 byte range,適合分段下載。
  • -s:靜音模式。
  • -#:顯示進度條。
  • -u:FTP 或 HTTP 用戶認證。
  • -T:上傳文件。

常見示例

抓取頁面:

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 輸出變量

-w 可用來輸出傳輸統計資訊,常見變量包括:

  • url_effective:最後請求的 URL。
  • http_code:HTTP 狀態碼。
  • time_total:總耗时。
  • time_namelookup:DNS 解析時間。
  • time_connect:建立連線時間。
  • time_starttransfer:開始傳輸時間。
  • size_download:下載大小。
  • size_upload:上傳大小。
  • speed_download:下載速度。
  • speed_upload:上傳速度。
  • content_type:回應內容類型。
  • num_redirects:重定向次數。

使用方式:

curl -w "http_code=%{http_code} time_total=%{time_total}\n" -o /dev/null -s https://example.com

透過 curl,開發者可以快速完成文件抓取、表單提交、API 測試與傳輸效能分析。

Copyright © Linux教程網 All Rights Reserved