0
0

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.

[TAR|SSH]複数ファイルをディレクトリごとローカル・リモート間で一括転送

Last updated at Posted at 2021-11-16

ローカルからリモートへ

ローカルフォルダ内のファイルをリモートのサーバに一括転送。

tar -cf - ./* | ssh user@hostname tar -xf - -C public_html/dir

tarを使うのは転送効率がよいから。普通にファイルを転送する場合、ファイル数が1万個あれば1万回のハンドシェイクが発生します。tarでひとつの書庫にまとめると、ハンドシェイクは1回で済みます。

-cf - は、書庫を「createする」「file名を-とする」という意味。

- (半角ハイフンひとつ) が意味するものは「標準出力」で、普通はハイフンではなくファイル名を指定します。

./* は「カレントフォルダの中身全部」です。サブフォルダも全部まとめます。

書庫データは標準入力を通じてsshに渡します。半角ハイフンひとつの - で受け取ります。

ssh user@hostname の後ろの文字列は、SSH接続後にそのままコマンド文字列として実行されます。

転送に失敗する場合

tar: Damaged tar archive
tar: Retrying...
tar: Damaged tar archive
tar: Retrying...
tar: Damaged tar archive
tar: Retrying...
tar: Error exit delayed from previous errors.

「アーカイブが壊れている」みたいなメッセージが多数表示されることがあります。もしPowerShellを使っている場合は、Bashなど他のシェルを試してみてください。

リモートからローカル(手元のPC)へ

ssh user@hostname "cd public_html/dir; tar -zcf - *" | tar -zxf -

リモートからローカルに転送する場合は上記のように実行します。

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?