2
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 1 year has passed since last update.

【PowerShell】複数のファイルに対して効率よくtarを実行する方法

Last updated at Posted at 2023-08-27

はじめに

Windowsでも地味にtarが使えます。
ただ、tarについてはUNIX系の記事が多いためあまりPowershellを前提とした記事が少ないと思います。

Powershellとtarを組み合わせて効率的な処理していくtips集のようなものを備忘として残していきたいと思います。

カレントディレクトリに存在する複数のファイルとディレクトリをアーカイブ・圧縮

$target=gci ; for($i=0; $i -lt ($target).Length ; $i++){tar cvzf test.tgz $target.name}

対象となるファイルが存在するディレクトリに移動して、
このワンライナーを実行することでカレントディレクトリ内のファイルをアーカイブ・gzip圧縮します。

尚、アーカイブファイル名は暫定的に"test.tgz"としています。

カレントディレクトリに存在する複数のtar.gz(tgz)を解凍するワンライナー

$target=gci ; for($i=0; $i -lt ($target).Length ; $i++){tar xvzf $target[$i]}

カレントディレクトリ内の複数のアーカイブファイルに対して解凍を行う場合に使用します。
解凍したい複数のアーカイブファイルを一つのディレクトリに纏めておき実行するといったケースで使えます。

注意点としては、tarの仕様として解凍先に同名のファイルやディレクトリが存在する場合に警告無しで上書きしてしまう点です。

事前に解凍後のファイル名と同名ファイルが存在するかの判定処理をいれるとよりセキュアになりますね、、、

再帰的に解凍させる

$target=gci -Recurse
for($i=0; $i -lt ($target).Length ; $i++){
    if (-not ($target[$i].psiscontainer)) {
        tar xvzf $target[$i].FullName
        }
    }

複数のディレクト配下にアーカイブファイルが存在するといったケースで使用します。

さすがにワンライナーでは分かりづらくなるためスクリプトっぽく書きました。

おわりに

以後、必要に応じてコマンドの追加・修正など行っていこうと思います。

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