1

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?

2
  • 1
    But the bash shell doesn't use regular expressions, it uses its own expansion rules. Jul 31, 2012 at 19:06
  • 1
    With Zsh you don't get ".." and "." when you do ".*" :) Jul 31, 2012 at 19:29

2 Answers 2

7

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.

0
1

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.

1
  • 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, 2012 at 19:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .