Linux 从查找中排除文件类型的正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6745401/
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
regular expression to exclude filetypes from find
提问by Michael
When using find
command in linux, one can add a -regex
flag that uses emacs regualr expressions to match.
find
在 linux 中使用command 时,可以添加一个-regex
使用 emacs regualr 表达式匹配的标志。
I want find to look for all files except .jar
files and .ear
files. what would be the regular expression in this case?
我想查找除.jar
文件和.ear
文件之外的所有文件。在这种情况下,正则表达式是什么?
Thanks
谢谢
采纳答案by dogbane
You don't need a regex here. You can use find
with the -name
and -not
options:
这里不需要正则表达式。您可以find
与-name
和-not
选项一起使用:
find . -not -name "*.jar" -not -name "*.ear"
A more concise (but less readable) version of the above is:
上面更简洁(但可读性较差)的版本是:
find . ! \( -name "*.jar" -o -name "*.ear" \)
回答by Tim Pietzcker
EDIT: New approach:
编辑:新方法:
Since POSIX regexes don't support lookaround, you need to negate the match result:
由于 POSIX 正则表达式不支持环视,您需要否定匹配结果:
find . -not -regex ".*\.[je]ar"
The previously posted answer uses lookbehind and thus won't work here, but here it is for completeness' sake:
先前发布的答案使用后视,因此在这里不起作用,但为了完整起见,这里是:
.*(?<!\.[je]ar)$
回答by Naftis
Using a regular expression in this case sounds like an overkill (you could just check if the name ends with something). I'm not sure about emacs syntax, but something like this should be generic enough to work:
在这种情况下使用正则表达式听起来有点矫枉过正(您可以只检查名称是否以某些内容结尾)。我不确定 emacs 语法,但像这样的东西应该足够通用:
\.(?!((jar$)|(ear$)))
i.e. find a dot (.) not followed by ending ($) "jar" or (|) "ear".
即找到一个点 (.) 后面没有结尾 ($) "jar" 或 (|) "ear"。
回答by user2268788
find . -regextype posix-extended -not -regex ".*\.(jar|ear)"
This will do the job, and I personally find it a bit clearer than some of the other solutions. Unfortunately the -regextype is required (cluttering up an otherwise simple command) to make the capturing group work.
这将完成这项工作,我个人认为它比其他一些解决方案更清晰。不幸的是,需要 -regextype(使其他简单的命令变得混乱)才能使捕获组工作。