正则表达式匹配linux“find”命令中的任一字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19111067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:58:48  来源:igfitidea点击:

regex match either string in linux "find" command

regexlinuxfind

提问by Reut Sharabani

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

我正在尝试使用以下方法递归查找以.pyor结尾的文件.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 regexdoes not?

一个简单的find -name "*.py"工作,为什么这regex不起作用?

采纳答案by devnull

Say:

说:

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

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

这样说会导致文件名匹配*.py*.py.server.

From man find:

来自man find

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

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

编辑:如果要指定正则表达式,请使用以下-regex选项:

find . -type f -regex ".*\.\(py\|py\.server\)"

回答by Chris Seymour

Find can take a regular expression pattern:

Find 可以采用正则表达式模式:

$ 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.

-正则表达式模式

文件名匹配正则表达式模式。这是对整个路径的匹配,而不是搜索。例如,匹配名为./fubar3', you can use the regular expression.*bar.'的文件。或 .*b.*3', but notf.*r3'。find 理解的正则表达式默认是 Emacs 正则表达式,但这可以通过 -regextype 选项进行更改。

-打印真;

在标准输出上打印完整的文件名,后跟换行符。如果您将 find 的输出传送到另一个程序中,并且您正在搜索的文件可能包含换行符的可能性很小,那么您应该认真考虑使用 -print0 选项而不是 -print。有关如何处理文件名中的异常字符的信息,请参阅 UNUSUAL FILENAMES 部分。

-regextype 类型

更改 -regex 和 -iregex 测试理解的正则表达式语法,这些测试稍后出现在命令行上。当前实现的类型是 emacs(这是默认的)、posix-awk、posix-basic、posix-egrep 和 posix-extended。

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

更清晰的描述或选项。不要忘记所有信息都可以通过阅读man find或找到info find

回答by Vorsprung

find -namedoes not use regexp, here's an extract from the man page on Ubuntu 12.04

find-name不使用正则表达式,这是 Ubuntu 12.04 手册页的摘录

-name pattern
              Base of  file  name  (the  path  with  the  leading  directories
              removed)  matches  shell  pattern  pattern.   The metacharacters
              (`*', `?', and `[]') match a `.' at the start of the  base  name
              (this is a change in findutils-4.2.2; see section STANDARDS CON‐
              FORMANCE below).  To ignore a directory and the files under  it,
              use  -prune; see an example in the description of -path.  Braces
              are not recognised as being special, despite the fact that  some
              shells  including  Bash  imbue  braces with a special meaning in
              shell patterns.  The filename matching is performed with the use
              of  the  fnmatch(3)  library function.   Don't forget to enclose
              the pattern in quotes in order to protect it from  expansion  by
              the shell.

So the pattern that -nametakes is more like a shell glob and not at all like a regexp

所以采用的模式-name更像是一个 shell glob 而根本不像一个正则表达式

If I wanted to find by regexp I'd do something like

如果我想通过正则表达式找到我会做类似的事情

find . -type f -print | egrep 'stub(\.py|\.server)'