Ask Ubuntu is a question and answer site for Ubuntu users and developers. 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

It works fine when I copy all hidden files using regex:

cp -r source/\.[^.]* destination/

and it just copies exactly files beggining with a dot . .

However, I found the following works also:

cp -r source/.[^.]* destination/

In regular expression, doesn't dot . mean "any singular character"? So why does not the second command copy regular files (those not hidden) ?
Why is the dot . interpreted literally in cp command?

share|improve this question
But the bash shell doesn't use regular expressions, it uses its own expansion rules. – Marty Fried Jul 31 '12 at 19:06
With Zsh you don't get ".." and "." when you do ".*" :) – Janus Troelsen Jul 31 '12 at 19:29
up vote 7 down vote accepted

cp, as well as the rest of the built-in shell commands, accept and expand glob patters and not regular expressions. They are similar in some aspects, different in many. For example, the . is literal in globs, and any single character is matched with ?.

Your friend in this case is man 7 glob.

share|improve this answer
Thanks for your reply. It indeed relieved my confusion. – LYg Jul 31 '12 at 19:16

Bash doesn't use regular expressions at all, it uses its own rules for wildcard expansion (called "globbing". The dot is simply a character, and the '^' character negates whatever follows. so the first example matches files starting with a dot, not followed by a second dot, and the next one does the same; There is no need to escape the dot but it is accepted anyway.

Edit: After looking at the suggested "man 7 glob" by Marios, I was mistaken, I think, about the '^' character. It seems to say that [^.] would be undefined rather than a not character. Bash uses an exclamation point for negation.

share|improve this answer
Yes, thank you very much. – LYg Jul 31 '12 at 19:18
Oh, I really didn't know much about globbing, your comment cautioned me of the great difference between glob and regex. It seems that I need a lot of reading-manual work, thank you. – LYg Jul 31 '12 at 19:28

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.