Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

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

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

2 Answers 2

up vote 72 down vote accepted

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\)"
share|improve this answer
6  
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
    
@MCS - without them, it would match a literal ( or ) in the file name. –  Paul Tomblin Dec 9 '08 at 16:06
    
because they are emacs regular expressions by default. use -regextype to change that. –  hop 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
find . \( -name  \*.xls -o -name \*.csv \) -print
share|improve this answer
    
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
1  
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.