Everything I read says that to exclude .svn and .htaccess and other hidden files when creating a tar archive, use the --exclude=".*" pattern.

When I try, I get an empty archive. When I leave out the --exclude long option everything gets archived.

Here's the full command I'm using:

tar -czvf ../_migrate/archive_2012-05-07.tgz --exclude=".*" ./*

I've also tried this variant, with no difference in results:

tar -czvf ../_migrate/archive_2012-05-07.tgz --exclude=".?*" ./*
  • Because you are telling it to exclude . and everything reachable through it... – vonbrand Jan 23 '13 at 16:29

You appear to be using GNU tar. Pattern matching in GNU tar works on the entire path, and does not stop at / characters. Since you are using ./ for your file list, that means every single file will match ./* which also matches .?*. I'd try something like --exclude='.[^/]*' perhaps.

  • Changing my command to tar -czvf ../_migrate/archive_2012-05-07.tgz exclude=".*" * fixed the problem, based on your answer. I'm surprised that it makes a difference, since in my mind * is the same as ./*, but apparently the './` gets added to the tar path and so everything falls under the pattern. So just to be clear, the exclude pattern was fine, it's the file list pattern that was causing the problem. – Tom Auger May 8 '12 at 15:27

Your pattern excludes ".", which is the directory you're trying to archive. Use ".?*" as the pattern instead.

  • I figured that, and tried using the ".?*" pattern, which ended up with the same results. But you've encouraged me to give it another try. I'll confirm. – Tom Auger May 8 '12 at 13:07
  • I can confirm that --exclude=".?*" is no different from ".*" in my case. – Tom Auger May 8 '12 at 13:22
  • Interesting, indeed. I tested on Mac OS Lion; tar --version yields "bsdtar 2.8.3 - libarchive 2.8.3". ".?*" works with this tar. – Kyle Jones May 8 '12 at 18:35
  • Sounds like the GNU tar handles either the pattern matching or the interpretation of the file list a little differently from bsdtar... – Tom Auger May 8 '12 at 19:55

.* will always match any file that would be included, as you are using files from . (which even by itself matches .*).

You do not need to do anything to exclude the files that you mention, they won't be matched by the glob anyway. The * glob does not match dot-prefixed files unless you manually enable such functionality (through dotglob, or your shell's equivalent).

  • You're not completely right on the dot part, although * does not match hidden files unless dotglob is set, subdirectories can still contain hidden files which are happily tarred if not using an exclude rule. – Lekensteyn May 8 '12 at 8:15
  • Ah! True, I hadn't considered that. – Chris Down May 8 '12 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.