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?

  • 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: unix.stackexchange.com/questions/83661/… – 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
  • 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
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

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.