使用bsdtar“--exclude”,如何仅排除子目录?

使用bsdtar“--exclude”,如何仅排除子目录?

在 GNU tar (即gtar)中,--exclude带有 glob 的选项仅匹配子目录,而不匹配目录本身。例如,--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.29bsdtar 3.3.2

答案1

如果有人正在寻找这个问题的答案,那很简单:在后面的正斜杠上添加一个反斜杠。

命令如下所示:

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

相关内容