BusyBox 的 tar --exclude 不排除

BusyBox 的 tar --exclude 不排除

dir1/dir2/file使用switch提取包含 content 的 tar 文件--exclude dir2。 gnu tar 被排除,但 busybox 的 tar 没有。

使用--exclude dir1/dir2效果很好,但是为什么呢?有没有办法排除任何dir2以 busybox 的 tar 命名的目录?

这里的test.sh:

# busybox 1.36 tested on debian:11 livecd and fedora:37 livecd
busybox

# clear old files
rm -rf bb-test

# prepair for testing
mkdir -p bb-test/dir1/dir2
cd bb-test
echo file3-content > dir1/dir2/file3
tar -cf a.tar dir1
mkdir out-coreut-2 out-busybo-2

# let's try!
        tar -xf a.tar -C out-coreut-2 --exclude dir2
busybox tar -xf a.tar -C out-busybo-2 --exclude dir2

# see difference
echo "" && tree out-coreut-2
echo "" && tree out-busybo-2

cd ..

我的机器上的输出:

BusyBox v1.36.0 (2023-01-10 00:00:00 UTC) multi-call binary.
...

out-coreut-2
└── dir1

2 directories, 0 files

out-busybo-2
└── dir1
    └── dir2
        └── file3

3 directories, 1 file

答案1

使用 --exclude dir1/dir2 工作正常,但为什么呢?

busybox tar 没有--exclude选项。请参阅 busybox(1)。所以它不能也不会起作用。在我安装的 Debian 11 上,如果我使用,--exclude我会收到以下错误消息:

tar: unrecognized option '--exclude'
BusyBox v1.30.1 (Debian 1:1.30.1-6+b3) multi-call binary.

代码的输出(除了错误消息)是

out-coreut-2
└── dir1

1 directory, 0 files

out-busybo-2

0 directories, 0 files

但请注意,错误的输入可能会给您带来一些结果:

busybox tar xf a.tar -C out-busybo-2 exclude dir1/dir2

这要求提取dir1/dir2exclude。排除在存档中不存在,有错误消息,但 dir1/dir2 确实存在于存档中并被提取。

有没有办法用 busybox 的 tar 排除任何名为 dir2 的目录?

不,不是用简单的命令。

您可以busybox rm -rf dir1/dir2在提取后进行操作,前提是您要将文件提取到的设备上有足够的空间。

如果 dir1 目录中还有其他内容,您最终可以列出存档、过滤掉^dir1/dir2并将结果用作要提取的文件列表。像这样的东西

# prepare data
rm -rf dir1
mkdir -p dir1/dir2 dir1/dir3
: >dir1/file1
: >dir1/dir2/file2
: >dir1/dir3/file3
busybox tar cf b.tar dir1
rm -rf dir1
# extract everything but dir1/dir2
busybox rm -rf out-busybo-2
busybox mkdir out-busybo-2
busybox tar xC out-busybo-2 -f b.tar $(
  busybox tar tf b.tar |
    busybox grep -ve ^dir1/dir2/ -e '^dir1/$' -e '/.*/.'
)

结果 :

out-busybo-2/
└── dir1
    ├── dir3
    │   └── file3
    └── file1

但是,如果存档中的文件名带有空格,则此操作将不起作用。此外,dir1 中至少需要有一个其他元素才能正常工作。并且可能还有其他一些极端情况会失败。

-e '/.*/.'添加以限制 tar tf 命令生成的元素数量。

答案2

不存在这样的“busybox 的 tar”。tar是一个二进制文件,包含大量模式、选项等。 Busybox 是一个二进制文件,它能够假装它是另一个二进制文件,例如tar.它通常用于嵌入式系统或类似系统中,只是为了节省空间。作为一个多才多艺的二进制文件,它只包含它所替换的原始二进制文件中可用的有限功能集。

至于问题,这似乎有效:

busybox tar -xf a.tar -C out-busybo-2 --exclude */dir2

结果:

out-coreut-2
└── dir1

1 directory, 0 files

out-busybo-2
└── dir1

1 directory, 0 files

相关内容