74

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

4

6 回答 6

111

就像是:

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

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

于 2011-03-04T22:54:28.730 回答
23

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

请注意存档文件之间gnutar的细微差别。bsdtar

# 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 -
于 2012-03-02T13:40:21.727 回答
18

扩展geekosaur 答案

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

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

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

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

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

于 2012-12-28T17:47:50.340 回答
3

您可以使用反引号而不是使用管道,例如:

tar cvzf archive.tgz `ls -1 *`

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

于 2010-04-08T06:36:53.633 回答
3
find /directory > filename
tar -T filename -cf archive.tar
于 2011-11-23T03:10:51.167 回答
2

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 ...
于 2018-06-07T01:26:03.813 回答