72

How can I use find to find all files that have a .xls or .csv extension? I have seen a -regex option but I don't know how to use it.

2 Answers 2

124

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\)"
9
  • 9
    Better answer than mine. +1. Dec 9, 2008 at 16:04
  • 1
    Why is a backslash needed before the parenthesis? I know it doesn't work without it, but it seems like it should.
    – MCS
    Dec 9, 2008 at 16:05
  • 1
    because they are emacs regular expressions by default. use -regextype to change that.
    – user601
    Dec 9, 2008 at 16:15
  • 4
    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. Dec 9, 2008 at 16:49
  • 2
    regex example does not work
    – hostmaster
    Jul 8, 2015 at 15:10
14
find . \( -name  \*.xls -o -name \*.csv \) -print
6
  • Didn't know about the -o option. Thanks!
    – MCS
    Dec 9, 2008 at 16:02
  • 1
    Does it need parentheses? find . '(' -name *.xls -o -name *.csv ')'-print
    – Adrian Pronk
    Dec 9, 2008 at 21:48
  • @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. Dec 10, 2008 at 13:09
  • 5
    Well, GNU find ( default on ubuntu 11.04) works correctly with both parens and -print OR without (parens and -print). But find . -name \*.xls -o -name \*.csv -print outputs only files matching .csv,
    – bbaja42
    Jan 2, 2012 at 14:20
  • You're right, I need the parens. Jan 2, 2012 at 15:02

You must log in to answer this question.