开发者

regular expression to exclude filetypes from find

When using find command in linux, one can add a -regex flag that uses emacs regualr expressions to match.

I want find to look for all files except .jar fi开发者_运维技巧les and .ear files. what would be the regular expression in this case?

Thanks


You don't need a regex here. You can use find with the -name and -not options:

find . -not -name "*.jar" -not -name "*.ear"

A more concise (but less readable) version of the above is:

find . ! \( -name "*.jar" -o -name "*.ear" \)


EDIT: New approach:

Since POSIX regexes don't support lookaround, you need to negate the match result:

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


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.


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:

\.(?!((jar$)|(ear$)))

i.e. find a dot (.) not followed by ending ($) "jar" or (|) "ear".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜