Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find command with regex for multiple file extension

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.

like image 962
user3282191 Avatar asked Mar 25 '14 03:03

user3282191


People also ask

Which search options can be used with the Find command?

The find Command It supports searching by file, folder, name, creation date, modification date, owner and permissions.

How do I view all PNG files?

Click My PC on the left-pane in File Explorer, or Computer in Windows Explorer. Enter the command kind:=picture into the search box to search all partitions on your hard drive for images saved in JPEG, PNG, GIF and BMP formats.


1 Answers

I see in the comments that you found out how to escape it with GNU basic regexes via the nonstandard flag 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 '*.xml' -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).

like image 184
Adam Katz Avatar answered Oct 12 '22 16:10

Adam Katz