在 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.29
和bsdtar 3.3.2
。
答案1
如果有人正在尋找這個問題的答案,那很簡單:在後面的正斜線後面加上一個反斜線。
命令如下圖所示:
bsdtar -xzf test.tgz --exclude 'test-tar/a/b\/*'