[英]regex match either string in linux “find” command
我正在嘗試以下方法以遞歸方式查找以.py
或.py.server
結尾的文件:
$ find -name "stub*.py(|\.server)"
但是這不起作用。
我嘗試過各種變化:
$ find -name "stub*.(py|py\.server)"
它們也不起作用。
一個簡單的find -name "*.py"
確實有效,那么這個regex
怎么沒有呢?
說:
find . \( -name "*.py" -o -name "*.py.server" \)
這樣說會導致文件名匹配*.py
和*.py.server
。
從man find
:
expr1 -o expr2
Or; expr2 is not evaluated if expr1 is true.
編輯:如果要指定正則表達式,請使用-regex
選項:
find . -type f -regex ".*\.\(py\|py\.server\)"
查找可以采用正則表達式模式:
$ find . -regextype posix-extended -regex '.*[.]py([.]server)?$' -print
選項:
-regex模式
文件名與正則表達式模式匹配。 這是整個路徑上的匹配,而不是搜索。 例如,要匹配名為
./fubar3', you can use the regular expression
的文件./fubar3', you can use the regular expression
。* bar。' 或.*b.*3', but not
f。* r3'。 find所理解的正則表達式默認為Emacs Regular Expressions,但可以使用-regextype選項進行更改。-print True;
在標准輸出上打印完整文件名,然后換行。 如果您將find的輸出傳遞給另一個程序,並且您搜索的文件可能包含換行符的可能性最小,那么您應該認真考慮使用-print0選項而不是-print。 有關如何處理文件名中的異常字符的信息,請參見“異常文件”部分。
-regextype類型
更改-regex和-iregex測試所理解的正則表達式語法,這些語法稍后會出現在命令行中。 當前實現的類型是emacs(這是默認值),posix-awk,posix-basic,posix-egrep和posix-extended。
更清晰的描述或選項。 不要忘記通過閱讀man find
或info find
可以找到所有信息。
find -name
不使用regexp,這是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.
所以-name
采用的模式更像是一個shell glob,而不像regexp
如果我想通過regexp找到我會做的事情
find . -type f -print | egrep 'stub(\.py|\.server)'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.