Find command with regex for multiple file extension

user3282191 picture user3282191 · Mar 25, 2014 · Viewed 15.8k times · Source

I was wondering if this is possible in find command. I am trying to find all the specific files with the following extensions then it will be SED after. Here's my current command script:

find . -regex '.*\.(sh|ini|conf|vhost|xml|php)$' | xargs sed -i -e 's/%%MEFIRST%%/mefirst/g'

unfortunately, I'm not that familiar in regex but something like this is what I need.

Answer

Adam Katz picture Adam Katz · Apr 29, 2014

I see in the comments that you found out how to escape it with standard find -regex RE, but you can also specify a type of regex that supports it without any escapes, making it a bit more legible:

In GNU findutils (Linux), use -regextype posix-extended:

find . -regextype posix-extended -regex '.*\.(sh|ini|conf|vhost|xml|php)$' | …

In BSD find (FreeBSD find or Mac OS X find), use -E:

find . -E -regex '.*\.(sh|ini|conf|vhost|xml|php)$' | …

The POSIX spec for find does not support regular expressions at all, but it does support wildcard globbing and -or, so you could be fully portable with this verbose monster:

find . -name '*.sh' -or -name '*.ini' -or -name '*.conf' \
  -or -name '*.vhost' -or -name '*.php' | …

Be sure those globs are quoted, otherwise the shell will expand them prematurely and you'll get syntax errors (since e.g. find -name a.sh b.sh … doesn't insert a -or -name between the two matched files and it won't expand to files in subdirectories).