2

I was under the impression that the * glob does not match dot-prefixed files unless you manually enable such functionality (through dotglob, or your shell's equivalent).

Yet if I have a directory a containing files file1 and .hidden1, then if I do

tar -cvpjf backup.tar.bz2 --exclude 'a/*' a

the resulting tar file contains only the empty directory a and no trace of either the hidden or non-hidden file. My expectation would have been only the non-hidden file1 would have been excluded.

What is going on here?

n.st
  • 7,918
  • 4
  • 35
  • 53
fpghost
  • 727
  • 2
  • 8
  • 21
  • 1
    Since you're using single quotes around `a/*`, the wildcard is expanded by `tar`, not your shell. It's manpage doesn't state whether `*` matches dotfiles, but from your experience, it looks like it. – n.st Jan 11 '14 at 22:47
  • This might help you: http://unix.stackexchange.com/questions/83661/creating-archive-using-tar-including-all-dotfiles-but-excluding-all-subdirecto – n.st Jan 11 '14 at 22:48
  • Agreed that the wildcard is expanded by `tar` because of the single quotes, but my reading of man page suggests it expects `*` not to match dotfiles: `Periods (‘.’) or forward slashes (‘/’) are not considered special for wildcard matches. However, if a pattern completely matches a directory prefix of a matched string, then it matches the full matched string: thus, excluding a directory also excludes all the files beneath it. ` – fpghost Jan 11 '14 at 22:49
  • Strange, you seem to have a different version of `tar`'s manpage than me... (GNU tar 1.26) – n.st Jan 11 '14 at 22:51
  • I was reading tar 1.27 man page, sec 6.5, online, but appear to be using version 1.26! Maybe therein lies the problem. Did they change this in the latest version for some reason... – fpghost Jan 11 '14 at 23:01
  • 1
    Actually, after thinking about it, I would understand `are not considered special for wildcard matches` as `are matched by wildcards`. So you will have to explicitly exclude files beginning with a dot from exclusion: `--exclude 'a/[^.]*'`. Just tested it and it seems to work. – n.st Jan 11 '14 at 23:05
  • Exactly, as the `man` page says. `.` are not considered special, therefore `*` matches `.` as well. – terdon Jan 11 '14 at 23:07
  • @terdon Oh, sorry, I read this as the complete opposite way around. All clear now. – fpghost Jan 11 '14 at 23:20

1 Answers1

1

First off, since you've enclosed the wildcard in single quotes, it is expanded by tar, instead of your shell, so its dotglob option will have no effect.

tar's * wildcard matches everything, including dots and slashes (as stated in the documentation you found), so you will have to exclude files starting with a dot from exclusion:

tar -cvpjf backup.tar.bz2 --exclude 'a/[^.]*' a
n.st
  • 7,918
  • 4
  • 35
  • 53