How can I build a tar from stdin?

后端 未结 6 915
攒了一身酷
攒了一身酷 2020-12-04 16:20

How can I pipe information into tar specifying the names of the file?

6条回答
  •  囚心锁ツ
    2020-12-04 17:00

    As already pointed out by geekosaur, there is no need to pipe the output of find to xargs because it is possible to pipe the output of find directly to tar using find ... -print0 | tar --null ....

    Note the slight differences between gnutar and bsdtar in excluding the archive file though.

    # 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 -
    

提交回复
热议问题