0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

curlとシェルスクリプトで連番になっている画像ファイルを取得

0
Posted at

環境

  • macOS Big Sur 11.0.1
  • GNU bash, version 3.2.57(1)-release
  • curl 7.64.1

curlで画像を取得する

画像の取得だけならcurlコマンドで実行できる。

terminal
curl -O %画像のURL%
# 例: curl -O http://hogehoge.com/fuga1.jpg

シェルスクリプトでforループする

例えば以下のような連続したファイルを一気に取得したい場合、curlコマンドを複数回実行するのはちょっとしんどい。

curl -O http://hogehoge.com/fuga1.jpg
curl -O http://hogehoge.com/fuga2.jpg
   :
   :
curl -O http://hogehoge.com/fuga90.jpg

シェルスクリプトでforを使うことで、手抜きします。
まずはシェルスクリプトのファイルを作成します。
terminal.appを開き、ダウンロードするディレクトリを作成/移動し、以下のコマンドでshファイルを作成します。

terminal
% vi download.sh

作成と同時にviが起動します。(操作方法は割愛。Qiitaには良質な記事があるのでそちらを参照)
以下を記載し、保存します。

download.sh
for((i=1; i<90; i++))
do
curl -O "http://hogehoge.com/fuga${i}.jpg" # ${i}でiを文字列として展開する
done

実行する時はbashからshファイルを指定して実行します。

terminal
% bash download.sh

こんな感じのメッセージがズラっと出てくればOK

terminal
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    65    0    65    0     0    160      0 --:--:-- --:--:-- --:--:--   160

注意

一般で公開されているサーバーへ軽率にぶっ放すとよろしくないので使い方は考えましょう。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?