Shell Command – Meaning of .??* Wildcard

tarwildcards

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?

Best Answer

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.

Related Question