tags:

views:

60

answers:

4

Hi I am trying to find all js & css files in one find command. I tried all of the below but in vain:

find WebContent -name "*.[jc]ss?"

find WebContent -name "*.[jc]ss{0,1}"

find WebContent -name "*.[jc][s]{1,2}$"

find WebContent -name "*.[jc]s{1,2}"

find WebContent -name "*.[jc]s[s]?"

Now what??

+3  A: 

-name accepts arguments that are globs, not regular expressions. You could use -regex if you wanted to use regular expressions, but the -o option (meaning "or") is probably the simplest solution:

find WebContent -name "*.js" -o -name "*.css"
Joachim Sauer
thanks to all, I guess I was just trying to dig deep into getting it working using reg exps. I got it working using :find WebContent -type f \( -name "*.css" -o -name "*.js" \)
rabbit
A: 

Try this

find WebContent -regextype posix-extended -regex '.*\.(css|js)$'
El Yobo
A: 

you can do boolean expressions with find. -o stands for OR.

find -name "*.js" -o -name "*.cpp"
Omry
-1: you need to either quote or escape those `*` s, e.g. `find -name \\*.js -o -name \\*.cpp` or `find -name "*.js" -o -name "*.cpp"`
Paul R
seems to work fine even when I don't.
Omry
@Omry: it will find matching files in the current directory but not in subdirectories, because the glob is expanded immediately - try it
Paul R
@Paul: It also "works" if you have no .js and no .css files in the current directory, because then the glob is expanded to itself.
Joachim Sauer
Joachim, you just answered a long standing question for me: why unquoted * in bash sometimes works and times not. I suspected it had something to do with files in the current dir but didn't know it expands to itself in such a case (which is a stupid thing to do in my opinion, it should expand to an empty string).
Omry
It's not so much that it expands to itself, as it just doesn't expand at all if nothing is matched - you can see the behaviour by trying something like "rm *.asdf", where it will try to remove the file called "*.asdf".
El Yobo
@Joachim: good point - that makes two potential failure modes where this may *appear* to work
Paul R
@Joachim, @Paul R: If you set the `nullglob` option in bash (using `shopt -s nullglob`), then the glob will be removed if it doesn't match anything.
Andrew Aylett
A: 

use the -iname option case insensitive

find WebContent \( -iname "*.js" -o -iname "*.css" \)
ghostdog74