Tar - Include Only Selected Sub-directories

Consider the situation where we want to create a compressed archive file for a directory using tar command. But we need to include only one or few subdirectories within a directory which already contains large number of other sub directories. Of course we can use --exclude option of tar, but we will end up having large number of --exclude options added in the command.

Consider that the directory which we are going to archive is administration, with following structure:

|-- India
|   |-- Kerala
|   |-- Tamil Nadu
|   `-- ...
`-- USA
    |-- Alabama
    |-- Arizona
    |-- California
    |-- Nevada
    `-- ...

To archive administration directory including all of its sub contents and to exclude USA states other than California. Normally, we can write a tar command containing --exclude options for each USA states except California. But it will become very long as we have to write down each USA state names. Thus it will be irritating to write all 50 USA state names like that.

We can avoid having above mentioned very long tar command by splitting it. Tar supports adding files to existing tar archive file, however it cannot add files to compressed archive files. So, first we create tar file excluding USA directory altogether without compression.

tar cvf administration.tar --exclude="USA" administration/

Then we add required subdirectories to already created tar file. In our case, we want to include California subdirectory under USA.

tar rvf administration.tar  administration/USA/California/

Please note that, in first command we used option c to create the archive file. On second command, we are using r option, which will add files/directories to existing archive file.

Now we can compress the archive file.

gzip administration.tar

I use this trick regularly at my work. Hopefully, sharing this, to help others facing similar requirements.