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.
Asked
Active
Viewed 1.4e+01k times
72
2 Answers
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\)"

Joachim Sauer
- 1,370
-
9Better answer than mine. +1. – Paul Tomblin Dec 09 '08 at 16:04
-
1Why is a backslash needed before the parenthesis? I know it doesn't work without it, but it seems like it should. – Dec 09 '08 at 16:05
-
@MCS - without them, it would match a literal ( or ) in the file name. – Paul Tomblin Dec 09 '08 at 16:06
-
1because they are emacs regular expressions by default. use -regextype to change that. – Dec 09 '08 at 16:15
-
4Just 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 09 '08 at 16:49
-
@saua - I'm glad I'm not the only one! – Paul Tomblin Dec 10 '08 at 13:10
-
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. – Dec 10 '08 at 18:24
-
2regex example does not work – hostmaster Jul 08 '15 at 15:10
-
1By the way, the find on OSX doesn't support the "|" operator in regex. http://stackoverflow.com/questions/5905493/why-isnt-this-regex-working-find-regex-m-h – Achal Dave Dec 06 '15 at 23:30
14
find . \( -name \*.xls -o -name \*.csv \) -print

Paul Tomblin
- 1,678
-
-
1
-
@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
-
5Well, 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 02 '12 at 14:20 -
-