Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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
1  
But the bash shell doesn't use regular expressions, it uses its own expansion rules. –  Marty Fried Jul 31 '12 at 19:06
1  
With Zsh you don't get ".." and "." when you do ".*" :) –  Janus Troelsen Jul 31 '12 at 19:29
add comment

2 Answers 2

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
add comment

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
add comment

Your Answer

 
discard

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.