Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The following command will tar all "dot" files and folders:

tar -zcvf dotfiles.tar.gz .??*

I am familiar with regular expressions, but I don't understand how to interpret .??*. I executed ls .??* and tree .??* and looked at the files which were listed. Why does this regular expression include all files within folders starting with . for example?

share|improve this question
see also unix.stackexchange.com/questions/1168/… – lesmana Jul 23 '12 at 20:37
up vote 31 down vote accepted

Globs are not regular expressions. In general, the shell will try to interpret anything you type on the command line that you don't quote as a glob. Shells are not required to support regular expressions at all (although in reality many of the fancier more modern ones do, e.g. the =~ regex match operator in the bash [[ construct).

The .??* is a glob. It matches any file name that begins with a literal dot ., followed by any two (not necessarily the same) characters, ??, followed by the regular expression equivalent of [^/]*, i.e. 0 or more characters that are not /.

For the full details of shell pathname expansion (the full name for "globbing"), see the POSIX spec.

share|improve this answer
Additional point: this is an attempt to write a glob which matches all of the dotfiles in a directory except the special entries . and .., which one normally does not want to do anything with. It's not quite right; it doesn't pick up anything named '.X' where X is some character other than dot. I don't think it's possible to write a single glob that matches every dotfile except . and .., but you can do it with two: tar zcvf dotfiles.tar.gz .[!.] .??* for instance. – zwol Jul 22 '12 at 21:45
@Zack: Thanks for the clarification. I posted a comment about that, but then deleted it. ls .? returned the same as ls .., which meant there were no other entries in the folder matching the pattern .?. I would have done .[^.] for all .? files other than ... – SabreWolfy Jul 23 '12 at 17:27
@SabreWolfy If you read the POSIX spec carefully, that's actually an important difference between globs and regex: in bracket expressions, [^abc] in regex syntax means the same as [!abc] in glob syntax (i.e. ^ is replaced with ! for globs). Using [^abc] style syntax in a glob is not very portable because POSIX does not specify what it means, so some shells interpret it using regex-like semantics while others treat ^ as just a literal character. – jw013 Jul 23 '12 at 17:31
@jw013: Thanks for the details. I must remember that glob != regexp :) – SabreWolfy Jul 23 '12 at 17:34
It just occurred to me that .[!.] may be left as a literal on the tar command line in the common case where there are no files that match that pattern. Some shells let you control that behavior, e.g. with bash, shopt -s nullglob will make it vanish from the command line if it doesn't match anything, but that's not a universal feature. – zwol Jul 23 '12 at 19:03

The .??* wildcard (not a regular expression, though it looks that way) translates into filenames that start with a period (.) , followed by two single characters (??), and then any number (zero or more) of other characters (*).

Maybe this page on Wildcards in Filenames will be helpful.

share|improve this answer

To add to the other answers, a single ? will translate to a single character filename and ?? will match filenames that has only two characters and so on.

[root@mercy testdir_2]# ls
ion  it  r
[root@mercy  testdir_2]# ls ?
[root@mercy  testdir_2]# ls ??
[root@mercy 1 testdir_2]# ls ???
[root@mercy  testdir_2]#
share|improve this answer
Downvoter, could you comment please? Would be happy to get corrected, if I am wrong. – Sreeraj Mar 26 '15 at 12:14
Your answer did not answer the question fully. – SabreWolfy Jul 13 at 14:08

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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