Wednesday, April 20, 2011

Exclude subdirectory when creating tar ball archive

When using GNU tar (commonly found in Linux distributions) getting tar to exclude a subdirectory can have some tricky pitfalls. Perform the tar command like this:

tar -zcvf mytarball.tar.gz --exclude "/home/user/directoryToTar/subdirAToExclude" --exclude "/home/user/directoryToTar/subdirBToExclude" /home/user/directoryToTar

Note the following:
  1. The --exclude must become before the file list of things to tar
  2. The directory to exclude must be surrounded by double quotes
  3. The directory to exclude must be a full system path (i.e. not a relative path, such as ./subDirAToExclude)
  4. The files to include must also be a full system path (i.e. not a relative path, such as ./directoryToTar)

No comments: