1
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 3 years have passed since last update.

階層構造を維持したままcurlコマンドでアップロードする方法

Posted at

今日はcurlコマンドで複数のファイルをアップロードする方法を紹介します。
通常curlコマンドはファイルしかアップロード、ダウンロードすることができず、ディレクトリ単位で操作することができません。
そのためディレクトリのアップロードを行う場合は少々工夫が必要となります。
通常ディレクトリ単位でのデータのやりとりにcurlを使用する事があまりないためか需要はないかもしれませんが、例えばArtifactory等のサーバーへ特定ディレクトリ配下のファイルをまとめてcurlで処理したい場合に役に立つと思います。

Shell中身

シェルの内容は、前回の応用となっています。
以下のシェルを処理対象ディレクトリの一つ上のディレクトリに作成し、そのディレクトリへcdコマンドで移動し、./<シェルスクリプト名> <対象ディレクトリ名>の形式で実行すると、対応するcurlコマンドがバーっと表示されます。
echoコマンドと"を外せばそのままcurlコマンドが実行されます。

# !/bin/sh

if [ $# != 1 ]; then
 echo "引数を1つ指定してください"
 exit 1
fi

DIRCNT=`pwd | wc -m`
let DIRCNT++
WORKDIR=`pwd`'/'$1
ALLFILES=`pwd`'/'$1'/'*
FINDFILE=`find $ALLFILES -type f`

for NOWFILE in $FINDFILE;
do
 NOWDIR=`echo $NOWFILE | cut -c $DIRCNT-`
 echo "curl -T "$NOWFILE" http://<サーバーのFQDN or IP>/"$NOWDIR
done

exit 0

スクリプトの解説は次回行います。

1
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
1
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?