67

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.

migrated from stackoverflow.com Jan 2 '12 at 13:06

This question came from our site for professional and enthusiast programmers.

115

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\)"
  • 8
    Better answer than mine. +1. – Paul Tomblin Dec 9 '08 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 '08 at 16:05
  • 1
    because they are emacs regular expressions by default. use -regextype to change that. – user601 Dec 9 '08 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. – Joachim Sauer Dec 9 '08 at 16:49
  • 1
    regex example does not work – hostmaster Jul 8 '15 at 15:10
12
find . \( -name  \*.xls -o -name \*.csv \) -print
  • Didn't know about the -o option. Thanks! – MCS Dec 9 '08 at 16:02
  • 1
    Does it need parentheses? find . '(' -name *.xls -o -name *.csv ')'-print – Adrian Pronk Dec 9 '08 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. – Paul Tomblin Dec 10 '08 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 '12 at 14:20
  • You're right, I need the parens. – Paul Tomblin Jan 2 '12 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy