Search code examples
subsetarchivetar

Write subset of a huge tar file to a new tar file without unpacking first


I have a huge gzipped tar archive file huge.tar.gz from which I want to extract a subset and put it in a new gzipped tar file archive subset.tar.gz, without first extracting all of the huge file. For example if listing the content of huge.tar.gz :

    tar tfz huge.tar.gz
    dir1/bla/bla.bla
    dir2/bla/bla.bla
    dir3/bla/bla.bla

, how can I create a new tar archive file subset.tar.gz containing only dir1:

    tar tfz subset.tar.gz
    dir1/bla/bla.bla

Solution

  • This is a two-step process. First you need to extract the directory from the tarball:

    tar -zxf huge.tar.gz dir1
    

    And then you need to compress it:

    tar -zcf subset.tar.gz dir1