如何从stdin构建tar?

时间:2010-04-08 06:04:13

标签: tar

如何将信息传递到指定文件名称的tar

7 个答案:

答案 0 :(得分:91)

类似的东西:

tar cfz foo.tgz -T -

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

答案 1 :(得分:22)

正如geekosaur已经指出的那样,没有必要将find的输出传递给xargs,因为可以将find的输出直接传递给tar使用find ... -print0 | tar --null ...

请注意,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 -

答案 2 :(得分:15)

扩展geekosaur answer

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

您可以将stdin与-T选项一起使用。

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

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

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

答案 3 :(得分:4)

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

tar cvzf archive.tgz `ls -1 *`

而不是ls -1 *,您可以放置​​任何其他命令来生成归档文件所需的列表

答案 4 :(得分:4)

find /directory > filename
tar -T filename -cf archive.tar

答案 5 :(得分:0)

为了更好的压缩,使用bzip2可能会有所帮助。

find $PWD -name "*.doc" > doc.filelist
tar -cvjf jumbo.tar.bz2 -T doc.filelist

答案 6 :(得分:0)

tar计划已经以多种方式实施。例如,在IBM的Unix版本AIX上,tar使用-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 ...
相关问题