How can I build a tar from stdin?

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

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

相关标签:
6条回答
  • 2020-12-04 16:39

    Instead of using pipe you could use backticks, e.g.:

    tar cvzf archive.tgz `ls -1 *`
    

    Instead of ls -1 * you can put any other command which produces list of needed to archive files

    0 讨论(0)
  • 2020-12-04 16:42

    Extending geekosaur answer:

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

    You can use stdin with the -T option.

    Note that if you filter files using some condition (e.g. -name option) in general you need to exclude directories in the pipe, otherwise tar will process all their content, that is not what you want. So, use:

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

    If you don't use -type, all the content of directories matching "mypattern" will be added !

    0 讨论(0)
  • 2020-12-04 16:47

    Something like:

    tar cfz foo.tgz -T -
    

    But keep in mind that this won't work for all possible filenames; you should consider the --null option and feed tar from find -print0. (The xargs example won't quite work for large file lists because it will spawn multiple tar commands.)

    0 讨论(0)
  • 2020-12-04 16:59

    The tar program has been implemented in a variety of ways. For example, on IBM's version of Unix, AIX, tar uses the -L option rather than -T, and requires a file rather than allowing - to indicate stdin:

    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 ...
    
    0 讨论(0)
  • 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 -
    
    0 讨论(0)
  • 2020-12-04 17:04
    find /directory > filename
    tar -T filename -cf archive.tar
    
    0 讨论(0)
提交回复
热议问题