【问题标题】:How can I build a tar from stdin?如何从标准输入构建 tar?
【发布时间】:2011-02-05 13:46:10
【问题描述】:

如何将信息通过管道传输到tar 并指定文件名?

【问题讨论】:

  • 我认为应该勾选@geekosaur,因为它的回答更符合问题。
  • 我已更改所选答案,您是对的。

标签: tar


【解决方案1】:

类似:

tar cfz foo.tgz --files-from=-

但请记住,这不适用于所有可能的文件名;您应该考虑--null 选项并从find -print0 提供tar。 (xargs 示例不适用于大型文件列表,因为它会产生多个 tar 命令。)

【讨论】:

  • HP-UX 上没有“-T”选项。
  • @PeterMortensen 安装 gnu tar 还是 bsd tar?您需要检查它在 HP-UX 应用商店中可能使用的名称...
  • 请注意,如果您想在使用find-print0 选项和tartar--null 选项时通过sort 进行管道传输,您还需要提供-z 选项到sort
【解决方案2】:

正如 geekosaur 已经指出的,没有必要将find 的输出通过管道传输到xargs,因为可以使用find ... -print0 | tar --null ...find 的输出直接传输到tar

请注意 gnutarbsdtar 在排除存档文件方面的细微差别。

# exclude file.tar.gz anywhere in the directory tree to be tar'ed and compressed
find . -print0 | gnutar --null --exclude="file.tar.gz" --no-recursion -czf file.tar.gz --files-from -
find . -print0 | bsdtar --null --exclude="file.tar.gz" -n -czf file.tar.gz -T -

# bsdtar excludes ./file.tar.gz in current directory by default
# further file.tar.gz files in subdirectories will get included though
# bsdtar: ./file.tar.gz: Can't add archive to itself
find . -print0 | bsdtar --null -n -czf file.tar.gz -T -

# gnutar does not exclude ./file.tar.gz in current directory by default
find . -print0 | gnutar --null --no-recursion -czf file.tar.gz --files-from -

【讨论】:

  • 如果在当前目录中查找,那么只需使用 ../file.tar.gz 作为目标就足够了。
  • “gnutar”和“bsdtar”是字面意思吗?
【解决方案3】:

扩展geekosaur answer:

find /directory | tar -cf archive.tar -T -

您可以使用带有-T 选项的标准输入。

请注意,如果您使用某些条件(例如-name 选项)过滤文件,通常您需要在管道中排除目录,否则 tar 将处理它们的所有内容,这不是您想要的想。所以,使用:

find /directory ! -type d -name "mypattern" | tar -cf archive.tar -T -

如果不使用-type,则会添加所有匹配"mypattern"的目录内容!

【讨论】:

  • 我认为这种方法比其他方法更好,因为您可以通过管道将任何输出到标准输出
  • 您可能需要考虑使用find 来具体说明文件类型。具体来说,我会做find -type f。这将排除符号链接、字符设备等。如果您对空目录感兴趣而不是find -type f -o -type d
  • @JamesThomasMoon1979,使用find -path dir/to/exclude -prune不是更好的选择吗?而不是通过type f 避免目录?还有,你说mypattern,但不是patter,而是globbin,因为pattern更好find -regex "patter"
  • 而不是-type f 来避免目录,我推荐! -type d。这样做的好处是排除其他项目,如符号链接、fifos等。
【解决方案4】:

您可以使用反引号代替管道,例如:

tar cvzf archive.tgz `ls -1 *`

除了ls -1 *,您可以使用任何其他命令生成所需归档文件的列表

【讨论】:

  • 唯一的问题是,如果 ls 命令的输出长于 shell 允许的最大命令行大小,这将不起作用。在这种情况下,您必须按照其他答案之一所说的那样做;这允许列表任意长。此外,“find [...] -print0”允许您创建一个 tar 文件,其中包含带有特殊字符的成员,而 ls 方法没有。这种方法并不安全或普遍适用。
【解决方案5】:
find /directory > filename
tar -T filename -cf archive.tar

【讨论】:

【解决方案6】:

tar 计划已以多种方式实施。例如,在 IBM 的 Unix 版本上,AIXtar 使用 -L 选项而不是 -T,并且需要文件而不是允许 - 指示标准输入:

Usage: tar -{c|r|t|u|x} [ -BdDEFhilmopRUsvwZ ] [ -Number ] [ -f TarFil e ]
       [ -b Blocks ] [ -S [ Feet ] | [ Feet@Density ] | [ Blocksb ] ]
       [ -L InputList ] [-X ExcludeFile] [ -N Blocks ] [ -C Directory ] File ...
Usage: tar {c|r|t|u|x} [ bBdDEfFhilLXmNopRsSUvwZ[0-9] ] ]
       [ Blocks ] [ TarFile ] [ InputList ] [ ExcludeFile ]
       [ [ Feet ] | [ Feet@Density ] | [ Blocksb ] ] [-C Directory ] File ...

【讨论】:

    猜你喜欢
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    • 1970-01-01
    相关资源
    最近更新 更多