HowtoForgeに7月26日(米国時間)に掲載された記事「Linux curl Command Tutorial for Beginners (5 Examples)」が、データ転送ユーティリティ「curl」の使い方を紹介した。

curlはファイルのダウンロードやアップロード、Webサービスの利用などでよく使われるコマンドの1つ。豊富な機能を提供しており、Webブラウザを使って手動で実施しているような処理を自動化したい場合などにも利用される。

記事で取り上げているcurlの主な使い方は次のとおり。

curlの基本的な使い方

curl URL

curlによるファイルダウンロードの例その1

curl https://download.freebsd.org/ftp/releases/amd64/amd64/ISO-IMAGES/11.2/FreeBSD-11.2-RELEASE-amd64-disc1.iso.xz > FreeBSD-11.2-RELEASE-amd64-disc1.iso.xz

curlによるファイルダウンロードの例その2

curl -o FreeBSD-11.2-RELEASE-amd64-disc1.iso.xz https://download.freebsd.org/ftp/releases/amd64/amd64/ISO-IMAGES/11.2/FreeBSD-11.2-RELEASE-amd64-disc1.iso.xz

curlによるファイルダウンロードの例その3

curl -O https://download.freebsd.org/ftp/releases/amd64/amd64/ISO-IMAGES/11.2/FreeBSD-11.2-RELEASE-amd64-disc1.iso.xz

301 Moved Permanentlyに応じて移動先のコンテンツをダウンロードする方法

# curl http://www.oneplus.com
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
# curl -L http://www.oneplus.com | head
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   178  100   178    0     0    348      0 --:--:-- --:--:-- --:--:--   348
<!doctype html>
<html from="CMS:General">
<head>
  <meta charset="utf-8">
  <title>Never Settle - OnePlus (United States)</title>
<link rel="canonical" href="https://www.oneplus.com/" />
<link rel="alternate" hreflang="x-default" href="https://www.oneplus.com/" />
<link rel="alternate" hreflang="en-IN" href="https://www.oneplus.in/" />
<link rel="alternate" hreflang="en-AU" href="https://www.oneplus.com/au/" />
<link rel="alternate" hreflang="en-IE" href="https://www.oneplus.com/ie/" />
100  8820    0  8820    0     0   9810      0 --:--:-- --:--:-- --:--:--  9810
curl: (23) Failed writing body (0 != 1024)
# 

途中からダウンロードを再開する方法

curl -C - -O https://download.freebsd.org/ftp/releases/amd64/amd64/ISO-IMAGES/11.2/FreeBSD-11.2-RELEASE-amd64-disc1.iso.xz
  • curlコマンドの実行例

    curlコマンドの実行例

curlコマンドはここで紹介されている以外にも多くの機能を提供している。詳しい使い方はオンラインマニュアルを参照のこと。