bsdtar "--exclude"を使用してサブディレクトリのみを除外する方法は?

bsdtar

GNU tar(例:)gtarでは、--excludeglobオプションはディレクトリ自体ではなくサブディレクトリにのみ一致します。たとえば、--exclude test-tar/a/b/*内部のすべての項目は除外されますが、それ自体bは除外されませんb。ただし、bsdtarディレクトリ自体も含まれません。私の質問は、bsdtarこれに関してGNUでも同じことをする方法です。

以下は、問題を示すサンプルスクリプトです。

#!/usr/bin/env bash

echo -e "\nGiven an archive that looks like this:"
bsdtar -tf test.tgz

echo -e "\nExtract the archive excluding test-tar/a/b/* using gtar"
rm -rf test-tar
gtar -xzf test.tgz --exclude 'test-tar/a/b/*'
file test-tar/a/b
file test-tar/a/b/B.txt

echo -e "\nExtract the archive excluding test-tar/a/b/* using bsdtar"
rm -rf test-tar
bsdtar -xzf test.tgz --exclude 'test-tar/a/b/*'
file test-tar/a/b
file test-tar/a/b/B.txt

この出力は次のようになります。

Given an archive that looks like this:
test-tar/
test-tar/a/
test-tar/a/A.txt
test-tar/a/b/
test-tar/a/b/B.txt

Extract the archive excluding test-tar/a/b/* using gtar
test-tar/a/b: directory
test-tar/a/b/B.txt: cannot open `test-tar/a/b/B.txt' (No such file or directory)

Extract the archive excluding test-tar/a/b/* using bsdtar
test-tar/a/b: cannot open `test-tar/a/b' (No such file or directory)
test-tar/a/b/B.txt: cannot open `test-tar/a/b/B.txt' (No such file or directory)

私のバージョンtar (GNU tar) 1.29はとですbsdtar 3.3.2

ベストアンサー1

誰かがこの質問に対する答えを探しているのは簡単です。末尾のスラッシュにバックスラッシュを追加するだけです。

コマンドは次のとおりです。

bsdtar -xzf test.tgz --exclude 'test-tar/a/b\/*'

おすすめ記事