417

I was trying to get a list of all python and html files in a directory with the command find Documents -name "*.{py,html}".

Then along came the man page:

Braces within the pattern (‘{}’) are not considered to be special (that is, find . -name 'foo{1,2}' matches a file named foo{1,2}, not the files foo1 and foo2.

As this is part of a pipe-chain, I'd like to be able to specify which extensions it matches at runtime (no hardcoding). If find just can't do it, a perl one-liner (or similar) would be fine.

Edit: The answer I eventually came up with include all sorts of crap, and is a bit long as well, so I posted it as an answer to the original itch I was trying to scratch. Feel free to hack that up if you have better solutions.

3
  • Related: How to use find command to search for multiple extensions at Unix SE
    – kenorb
    Apr 13, 2015 at 13:46
  • 1
    An often overlooked and underused utility is also locate, albeit with the caveat that the internal updatedb may not be up-to-date. But it's fast.
    – michael
    Mar 12, 2018 at 10:01
  • 1
    I'm voting to close this question as off-topic because it belongs on Unix&Linux Apr 11, 2020 at 22:39

12 Answers 12

593

Use -o, which means "or":

find Documents \( -name "*.py" -o -name "*.html" \)

You'd need to build that command line programmatically, which isn't that easy.

Are you using bash (or Cygwin on Windows)? If you are, you should be able to do this:

ls **/*.py **/*.html

which might be easier to build programmatically.

10
  • 3
    I'm using zsh, which, as a general rule, supports all bashisms, plus more. Jul 15, 2009 at 20:50
  • 17
    Zsh supports ** for recursive search; Bash only supports it in versions 4.0 and up, and only with shopt -s globstar.
    – ephemient
    Jul 16, 2009 at 1:59
  • 2
    How many -o args can you have? I've got a potentially large list of .gcda (coverage data) files to build up Jan 4, 2014 at 8:01
  • 47
    You need to surround the two -names with brackets, if you're using -exec. E.g. find Documents \( -name "*.py" -o -name "*.html" \) -exec file {} \;
    – artbristol
    Mar 10, 2014 at 14:23
  • 3
    @artbristol comment is very relevant if, for example, you are adding a -print0 to handle filenames with spaces.
    – nimrodm
    Apr 28, 2015 at 11:51
85

Some editions of find, mostly on linux systems, possibly on others aswell support -regex and -regextype options, which finds files with names matching the regex.

for example

find . -regextype posix-egrep -regex ".*\.(py|html)$" 

should do the trick in the above example. However this is not a standard POSIX find function and is implementation dependent.

2
  • 22
    Simpler: find . -regex ".*\.\(py\|html\)$" this works because find defaults to Emacs-style regular expressions, which are slightly different, so you don't have to specify the regextype.
    – robru
    Apr 24, 2013 at 18:41
  • 2
    If you have many expressions -regextype posix-egrep is handy (otherwise you'd need to escape lots of characters). This is the find command I've used for a dist-hook building a Windows distribution zip (finds the files to change and in-file changes them to dos-eol): find -regextype posix-egrep -regex ".*(\.([chyl]|def|cpy|cob|conf|cfg)|(README|ChangeLog|AUTHORS|ABOUT-NLS|NEWS|THANKS|TODO|COPYING.*))$" -exec sed -i -e 's/\r*$/\r/' {} \; Aug 4, 2016 at 8:58
39

This will find all .c or .cpp files on linux

$ find . -name "*.c" -o -name "*.cpp"

You don't need the escaped parenthesis unless you are doing some additional mods. Here from the man page they are saying if the pattern matches, print it. Perhaps they are trying to control printing. In this case the -print acts as a conditional and becomes an "AND'd" conditional. It will prevent any .c files from being printed.

$ find .  -name "*.c" -o -name "*.cpp"  -print

But if you do like the original answer you can control the printing. This will find all .c files as well.

$ find . \( -name "*.c" -o -name "*.cpp" \) -print

One last example for all c/c++ source files

$ find . \( -name "*.c" -o -name "*.cpp"  -o -name "*.h" -o -name "*.hpp" \) -print
38

You could programmatically add more -name clauses, separated by -or:

find Documents \( -name "*.py" -or -name "*.html" \)

Or, go for a simple loop instead:

for F in Documents/*.{py,html}; do ...something with each '$F'... ; done
3
  • 2
    Is it a -or or a -o ?
    – Stephane
    Sep 5, 2014 at 10:04
  • 2
    @StephaneEybert: either is fine, but only the latter is POSIX compliant (according to the man page).
    – Stephan202
    Sep 6, 2014 at 14:33
  • This doesn't work recursively for me on macOS. As in for files in subdirectories. Mar 8, 2018 at 22:18
13

I had a similar need. This worked for me:

find ../../ \( -iname 'tmp' -o -iname 'vendor' \) -prune -o \( -iname '*.*rb' -o -iname '*.rjs' \) -print
1
  • I wanted to find files that matched *.c *.cpp or *.cc With only two -name patterns I didn't need parens but with three -name patterns joined with two -o patterns find -name "*.cpp" -o -name "*.c" -o -name "*.cc" -print0 I had to use a pair of parens to group the second or operator. find -name "*.cpp" -o \( -name "*.c" -o -name "*.cc" \) -print0 It may be that the -print0, which is always "true" affected the logic. Jul 19, 2016 at 22:00
4

My default has been:

find -type f | egrep -i "*.java|*.css|*.cs|*.sql"

Like the less process intencive find execution by Brendan Long and Stephan202 et al.:

find Documents \( -name "*.py" -or -name "*.html" \)

1
  • 3
    that's not a correct use of egrep regexp, rather, you've a shell glob where a regexp should be used. (Also, typical find usage is: find {directory} [options...] [action], where, depending on impl, directory may default to ., and action defaults to -print, but I'll be explicit.) So, instead, use something like: find . -type f -print | egrep -i '\.java$|\.css$|\.cs$|\.sql$' But also, as really fast alternative to find , one might also try locate in a similar fashion (albeit not necessarily up to date, since it queries an internal db for a file list)
    – michael
    Mar 12, 2018 at 9:57
3

Braces within the pattern \(\) is required for name pattern with or

find Documents -type f \( -name "*.py" -or -name "*.html" \)

While for the name pattern with and operator it is not required

find Documents -type f ! -name "*.py" -and ! -name "*.html" 
2
#! /bin/bash
filetypes="*.py *.xml"
for type in $filetypes
do
find Documents -name "$type"
done

simple but works :)

1

I needed to remove all files in child dirs except for some files. The following worked for me (three patterns specified):

find . -depth -type f -not -name *.itp -and -not -name *ane.gro -and -not -name *.top -exec rm '{}' +
0

This works on AIX korn shell.

find *.cbl *.dms -prune -type f -mtime -1

This is looking for *.cbl or *.dms which are 1 day old, in current directory only, skipping the sub-directories.

0
find MyDir -iname "*.[j][p][g]"
+
find MyDir -iname "*.[b][m][p]"
=
find MyDir -iname "*.[jb][pm][gp]"
1
  • 3
    Note that the latter will match foo.jmg but neither of the top two will.
    – copper.hat
    Dec 2, 2017 at 20:35
0

What about

ls {*.py,*.html}

It lists out all the files ending with .py or .html in their filenames

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.