Why does tar --exclude=".*" create an empty archive?

9,945

Solution 1

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.

Solution 2

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

Solution 3

.* 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).

Share:
9,945

Related videos on Youtube

Tom Auger
Author by

Tom Auger

Updated on September 18, 2022

Comments

  • Tom Auger
    Tom Auger almost 2 years

    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=".?*" ./*
    
    • vonbrand
      vonbrand over 11 years
      Because you are telling it to exclude . and everything reachable through it...
  • Lekensteyn
    Lekensteyn about 12 years
    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.
  • clerksx
    clerksx about 12 years
    Ah! True, I hadn't considered that.
  • Tom Auger
    Tom Auger about 12 years
    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
    Tom Auger about 12 years
    I can confirm that --exclude=".?*" is no different from ".*" in my case.
  • Tom Auger
    Tom Auger about 12 years
    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.
  • Kyle Jones
    Kyle Jones about 12 years
    Interesting, indeed. I tested on Mac OS Lion; tar --version yields "bsdtar 2.8.3 - libarchive 2.8.3". ".?*" works with this tar.
  • Tom Auger
    Tom Auger about 12 years
    Sounds like the GNU tar handles either the pattern matching or the interpretation of the file list a little differently from bsdtar...