Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

While we use * to denote zero or more previous characters in grep, we use *.c to find all C files when we use it with the ls command like ls *.c. Could someone tell how the use of * differs in these two cases?

share|improve this question
add comment

2 Answers 2

up vote 10 down vote accepted

Shell file name globbing and regular expressions use some of the same characters, and they have similar purposes, but you're right, they aren't compatible. File name globbing is a much less powerful system.

In file name globbing:

  • * means "zero or more characters"

  • ? means "any single character"

  • Square brackets ([]) appear to work just like regexes on the system I'm typing this on, including things like POSIX character classes (e.g. [:alpha:]). That said, if you need your commands to work on many different system types, I recommend against using anything beyond elementary things like lists of characters (e.g. [abeq]) and maybe character ranges (e.g. [a-c]).

There is a rough similarity between file globbing syntax and regexes, but if you need regex matching of file names, you need to do it another way. find -regex is one option. (Notice that there is also find -name, by the way, which uses glob syntax.)

share|improve this answer
    
I don't know it was called globbing :) –  user3539 Dec 8 '12 at 13:07
3  
In addition, there are various flavours of regex. Not all regexes are created the same! And you have many other pattern matching systems, such as SQL like, where '%' means '*'. –  Mr Lister Dec 8 '12 at 13:48
2  
Two major flavors of regexp are POSIX and PCRE (Perl Compatible R.E.). The later is less long-winded and has some more features. Unix tools and shells generally use POSIX, most programming languages with built-in regexps (except shell) use PCRE. Just beware the difference when you are reading material on-line. –  goldilocks Dec 8 '12 at 15:05
    
You answer what, but the question was why. –  reinierpost Dec 8 '12 at 16:51
    
@reinierpost But, in the question itself, specifically Could someone tell how the use of * differs in these two cases? was asked. –  Bernhard Jan 1 '13 at 8:58
show 1 more comment

Answering the title question (why):

file name expansion predates regular expressions, already existed with most operating systems (wildcard/joker characters) and is much simpler and intuitive than the latter.

While *.txt is easily understandable by casual users, the equivalent .*\.txt is something more targeted to experienced users/programmers.

share|improve this answer
1  
Another reason for the “why” part: speed. Regular expressions are slower: pastebin.com/3iNCgkE3 –  manatwork Jan 1 '13 at 11:21
    
*.txt doesn't equal .*\.txt, it (mostly) equals .*\.txt$ because there can be nothing after the .txt (at least assuming reasonable file name globbing). Perhaps even ^.*\.txt$ somewhat depending on usage. Proves your point? –  Michael Kjörling Jan 9 '13 at 8:17
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.