Why does tar --exclude=".*" create an empty archive?
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).
Related videos on Youtube
Tom Auger
Updated on September 18, 2022Comments
-
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 over 11 yearsBecause you are telling it to exclude
.
and everything reachable through it...
-
-
Lekensteyn about 12 yearsYou're not completely right on the dot part, although
*
does not match hidden files unlessdotglob
is set, subdirectories can still contain hidden files which are happily tarred if not using an exclude rule. -
clerksx about 12 yearsAh! True, I hadn't considered that.
-
Tom Auger about 12 yearsI 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 about 12 yearsI can confirm that --exclude=".?*" is no different from ".*" in my case.
-
Tom Auger about 12 yearsChanging 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 thetar
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 about 12 yearsInteresting, indeed. I tested on Mac OS Lion; tar --version yields "bsdtar 2.8.3 - libarchive 2.8.3". ".?*" works with this tar.
-
Tom Auger about 12 yearsSounds like the GNU tar handles either the pattern matching or the interpretation of the file list a little differently from bsdtar...