A Better TAR on Mac OS X - Excluding Save/Backup Files, SVN Metadata, and Resource Forks

posted Nov 11, 2009, 2:25 AM by Michael Safyan   [ updated Feb 19, 2012, 2:13 PM ]
If you have ever created a ".tar", ".tar.gz", or ".tar.bz2" archive on Mac OS X, you will notice that it includes files that begin with "._". Although tar provides a "--exclude" option, all attempts to exclude such files with that directive ultimately fail. Thanks to "tar, Leopard, and those evil, nasty resource forks" and "OS X, tar, and resource forks", there is a solution...

We will now apply the solution to creating shell scripts "tarbz2", "targz", "untarbz2", and "untargz" that will tar/untar while omitting these resource forks. As a bonus, we will also have the tar program ignore the '.DS_Store' file that Finder tends to generate, in addition to the ".svn" directories created by Subversion.

For "tarbz2' and "targz", we set the appropriate environment variables, then invoke tar with the appropriate --exclude options. The "tarbz2" program is as follows:

#! /bin/bash
tar -c --exclude='._*' --exclude='.svn' --exclude='.DS_Store' --exclude='*.bak' --exclude='*~' -vjf "$@"

You will notice that I throw in a couple extra excludes, including an additional '._*'; I do this so that I can also run the command under Linux. The "targz" file is as follows:

#! /bin/bash
tar -c --exclude='._*' --exclude='.svn' --exclude='.DS_Store' --exclude='*.bak' --exclude='*~' -vzf "$@"

The two scripts are identical, except that "j" is used for the bz2 variant, while "z" is used for the gz variant. The "untarbz2" program is as follows:

#! /bin/bash
tar -xvjf "$@"

There is nothing special to this command. However, for the sake of symmetry/consistency, I like to have it. The "untargz" program differs only in the use of "z" instead of "j":

#! /bin/bash
tar -xvzf "$@"

Assuming that these programs are made executable and placed in a folder that is in the executable search PATH, one can create a ".tar.bz2" archive using:

tarbz2 destination.tar.bz2 source1 [ source2 ... sourceN ]

Similarly, one can create a ".tar.gz" archive using:

targz destination.tar.gz source1 [ source2 ... sourceN ]

Both of these invocations will omit extraneous garbage files to which no user should be subjected. The "untarbz2" and "untargz" files may be used to decompress the resulting archives.