我有两个文件夹,CA01
和CA02
当前文件夹下foo
:
foo -+
|
+-CA01
|
+-CA02
当我打字
find . -regex ".*CA[0-9]+" -exec echo {} +
或者
find . -regex ".*CA[0-9][0-9]" -exec echo {} +
我有以下输出,这是预期的:
./CA01 ./CA02
但是当我输入
find . -regex ".*CA[0-9]\{2\}" -exec echo {} +
没有任何显示,这是非常出乎意料的。
因为默认情况下find
使用emacs正则表达式。我可以使用以上所有内容来匹配两个文件夹。
我在这里想念什么吗?
您需要将更-regextype
改为支持重复计数的值(即{2}
)。默认值emacs
似乎不支持计数。默认的正则表达式类型模仿没有重复计数语法的Emacs的较早版本。以下类型似乎对我有用。
posix-egrep
$ find foo -regextype posix-egrep -regex ".*CA[0-9]{2}" -exec echo {} +
foo/CA02 foo/CA01
和
$ find foo -regextype sed -regex ".*CA[0-9]\{2\}" -exec echo {} +
foo/CA02 foo/CA01
posix扩展
$ find foo -regextype posix-extended -regex ".*CA[0-9]{2}" -exec echo {} +
foo/CA02 foo/CA01
还有其他人,但我没有再尝试了。请参见find
手册页并搜索-regextype
。
摘抄
-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.
$ find -version
find (GNU findutils) 4.5.9
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Eric B. Decker, James Youngman, and Kevin Dalley.
Built using GNU gnulib version 1778ee9e7d0e150a37db66a0e51c1a56755aab4f
Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION FTS(FTS_CWDFD) CBO(level=2)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句