views:

2740

answers:

2

What's the regular expression I could use with find -regex to find all files that have a .xls or .csv extension?

+2  A: 
find . -name \*.xls -o -name \*.csv -print
Paul Tomblin
Didn't know about the -o option. Thanks!
MCS
Does it need parentheses? find . '(' -name \*.xls -o -name \*.csv ')'-print
Adrian Pronk
@Adrian - no, it doesn't. I'm not sure if it would need parens on a non-GNU find where "-print" wasn't already the default action.
Paul Tomblin
+14  A: 

Why not simply use this:

find -name "*.xls" -o -name "*.csv"

You don't need regex for this.

If you absolutely want to use regex simply use

find -regex ".*\.\(xls\|csv\)"
Joachim Sauer
Better answer than mine. +1.
Paul Tomblin
Why is a backslash needed before the parenthesis? I know it doesn't work without it, but it seems like it should.
MCS
@MCS - without them, it would match a literal ( or ) in the file name.
Paul Tomblin
because they are emacs regular expressions by default. use -regextype to change that.
hop
Just for the record: I can never remeber which tools want "\(" for grouping and which want "(". I always have to try it to know it.
Joachim Sauer
@saua - I'm glad I'm not the only one!
Paul Tomblin
If the file names have any special characters (whitespace, newlines, etc) the default -print may cause problems with shell expansion. To be absolutely safe you should use -print0 and xargs -0.
Hudson