What's the regular expression I could use with find -regex
to find all files that have a .xls or .csv extension?
views:
2740answers:
2Didn't know about the -o option. Thanks!
MCS
2008-12-09 16:02:46
Does it need parentheses? find . '(' -name \*.xls -o -name \*.csv ')'-print
Adrian Pronk
2008-12-09 21:48:22
@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
2008-12-10 13:09:41
+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
2008-12-09 16:01:40
Why is a backslash needed before the parenthesis? I know it doesn't work without it, but it seems like it should.
MCS
2008-12-09 16:05:01
@MCS - without them, it would match a literal ( or ) in the file name.
Paul Tomblin
2008-12-09 16:06:54
because they are emacs regular expressions by default. use -regextype to change that.
hop
2008-12-09 16:15:34
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
2008-12-09 16:49:14
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
2008-12-10 18:24:05