带有 --include 模式的 tar

标签 tar

以下是我尝试过的脚本的一个片段
压缩子目录中的所有 php 文件。它尝试使用“--include”参数
但似乎不起作用(输出来自 bash 中的“set -x”)

+ find . -name '*.php'
./autoload.php
./ext/thrift_protocol/run-tests.php
./protocol/TBinaryProtocol.php
...
./transport/TTransportFactory.php
+ tar -cvjf my.tar.bz2 '--include=*.php' .
+ set +x
find找到了几个 php 文件,但 tar 似乎没有看到它们。如果我取出 --include所有文件都被 tar 化。

我知道我可以使用 find 来提供列表
( find . -name '*.php' -print0 | tar -cvjf "my.tar.bz2" --null -T - ),但是 --include 有什么问题参数?

最佳答案

GNU tar 有一个 -T or --files-from option可以获取包含要包含的文件列表的文件。使用此选项指定的文件可以是标准输入的“-”。因此,您可以使用 将任意文件列表传递给 tar 以从标准输入存档。 -文件来自 - .使用 查找 模式来生成文件列表,您的示例变为:

find . -name '*.php' -print0 | tar -cvjf my.tar.bz2 --null --files-from -

关于带有 --include 模式的 tar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5747755/

相关文章:

Linux命令从tar中提取单个文件作为字节/字节数组

linux - 无法使用 tar 提取 nodejs 源文件

tar - 如何使用 7zip 创建 tar 文件

perl - 如何将 tar 文件解压到目标目录?

node.js - npm audit 任意文件覆盖

python - 压缩存档,可快速访问单个文件

linux - 从文件列表中获取输入的 Tar 归档

linux - 无法提取 tar.gz 文件

python - 如何使用 Python 检查文件是否存在于 tar 存档中?

unix - 帮助 unix tar 和 grep 循环