tar与--include模式

nhe*_*hed 25 tar

以下是我所拥有的脚本的片段,它试图对子目录中的所有php文件进行tar.它尝试使用'--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
Run Code Online (Sandbox Code Playgroud)

find发现了几个PHP文件,但焦油似乎并没有看到他们.如果我拿出--include所有文件都涂焦油.

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

Nic*_*kin 87

GNU tar有一个-T--files-from选项,可以获取包含要包含的模式列表的文件.对于stdin,使用此选项指定的文件可以是" - ".因此,您可以使用-files-from - 将 tar的任意文件列表传递给stdin .你的例子变成:

find . -name '*.php' | tar -cvjf my.tar.bz2 --files-from -
Run Code Online (Sandbox Code Playgroud)

  • 接受的答案对我不起作用,但这很棒! (3认同)
  • 此外,如果你想让匹配多个模式的东西进入tar文件,你可以这样做:find.-regex".*\.php"-o -regex".*\.css"| tar cvzf stuff.tgz --files-from - (3认同)

cni*_*tar 7

实际上我只是查看了tar(1)我的freebsd系统,我找到了一个--include选项(之前我曾在网上看过一些老人页面).该--include选项是相当强大的.这里有些例子

这些是文件

cnicutar@uranus ~/tar_test $ ls -1
a.c
b.c
x
Run Code Online (Sandbox Code Playgroud)

简单的tar,存档一切

cnicutar@uranus ~/tar_test $ tar -cvf archive1.tar *
a a.c
a b.c
a x
Run Code Online (Sandbox Code Playgroud)

仅归档C文件

cnicutar@uranus ~/tar_test $ tar -cvf archive2.tar --include='*.c' *
a a.c
a b.c
Run Code Online (Sandbox Code Playgroud)

所以你的脚本可能出错的是你给tar .而不是.*最后一个参数.

编辑

我试过了,感到很惊讶.这种行为tar(1)是意料之外的,但(我相信)是有意的.手册页说:

Process only files or directories that match the specified pattern.
Run Code Online (Sandbox Code Playgroud)

因此,当您指定模式时,它会筛选出与其不匹配的任何目录.因此,如果您的目录没有碰巧具有该扩展名(它有效但不常见),它将不会进入它们(即使内部可能存在"有趣"文件).

总而言之,我认为最好使用另一种方式来递归枚举+过滤文件.

  • 我的 Gnu tar `1.28` 也没有 `--include` 选项。 (7认同)
  • 从v1.30开始,gnu tar中不存在--include选项! (3认同)
  • 从 GNU 版本 1.32 开始,“tar”不包含“--include”标志。 (3认同)