如何从stdin构建tar?

Kri*_*ves 62 tar

如何管道信息以tar指定文件的名称?

gee*_*aur 97

就像是:

tar cfz foo.tgz -T -
Run Code Online (Sandbox Code Playgroud)

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


小智 22

正如geekosaur已经指出的那样,不需要输出findto 的输出,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 -
Run Code Online (Sandbox Code Playgroud)


小智 16

扩展geekosaur回答:

find /directory | tar -cf archive.tar -T -
Run Code Online (Sandbox Code Playgroud)

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

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

find /directory -type f -name "mypattern" | tar -cf archive.tar -T -
Run Code Online (Sandbox Code Playgroud)

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