Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex match either string in linux "find" command

Tags:

regex

linux

find

I'm trying the following to recursively look for files ending in either .py or .py.server:

$ find -name "stub*.py(|\.server)" 

However this does not work.

I've tried variations like:

$ find -name "stub*.(py|py\.server)" 

They do not work either.

A simple find -name "*.py" does work so how come this regex does not?

like image 614
Reut Sharabani Avatar asked Oct 01 '13 08:10

Reut Sharabani


People also ask

Can you use regex with find Linux?

Different regex types are available for the command find: findutils. emacs (which is the default option unless otherwise specified) gnu-awk.

Can I use regex with find?

To use regular expressions, open either the Find pane or the Replace pane and select the Use check box. When you next click Find Next, the search string is evaluated as a regular expression. When a regular expression contains characters, it usually means that the text being searched must match those characters.

Can we use regex in grep command?

GNU grep supports three regular expression syntaxes, Basic, Extended, and Perl-compatible. In its simplest form, when no regular expression type is given, grep interpret search patterns as basic regular expressions. To interpret the pattern as an extended regular expression, use the -E ( or --extended-regexp ) option.


2 Answers

Say:

find . \( -name "*.py" -o -name "*.py.server" \) 

Saying so would result in file names matching *.py and *.py.server.

From man find:

   expr1 -o expr2           Or; expr2 is not evaluated if expr1 is true. 

EDIT: If you want to specify a regex, use the -regex option:

find . -type f -regex ".*\.\(py\|py\.server\)" 
like image 80
devnull Avatar answered Sep 21 '22 18:09

devnull


Find can take a regular expression pattern:

$ find . -regextype posix-extended -regex '.*[.]py([.]server)?$' -print 

Options:

-regex pattern

File name matches regular expression pattern. This is a match on the whole path, not a search. For example, to match a file named ./fubar3', you can use the regular expression.*bar.' or .*b.*3', but notf.*r3'. The regular expressions understood by find are by default Emacs Regular Expressions, but this can be changed with the -regextype option.

-print True;

print the full file name on the standard output, followed by a newline. If you are piping the output of find into another program and there is the faintest possibility that the files which you are searching for might contain a newline, then you should seriously consider using the -print0 option instead of -print. See the UNUSUAL FILENAMES section for information about how unusual characters in filenames are handled.

-regextype type

Changes the regular expression syntax understood by -regex and -iregex tests which occur later on the command line. Currently-implemented types are emacs (this is the default), posix-awk, posix- basic, posix-egrep and posix-extended.

A clearer description or the options. Don't forgot all the information can be found by reading man find or info find.

like image 24
Chris Seymour Avatar answered Sep 23 '22 18:09

Chris Seymour