简体   繁体   English

正则表达式-查找不匹配的文件

[英]regex - find files that don't match

I have a list of directories where most are in a format like ./[foobar]/ . 我有一个目录列表,其中大多数目录都采用./[foobar]/类的格式。 However, some are formatted like ./[foo] bar/ . 但是,有些格式如./[foo] bar/

I would like to use find (or some other utility my shell offers) to find those directories not matching the first pattern (ie having text outside the square braces). 我想使用find (或shell提供的其他实用程序)来查找与第一个模式匹配的目录(即,在方括号外有文本)。 Until now, I was unable to find a way to "inverse" my pattern. 直到现在,我还无法找到一种方法来“反转”我的模式。

Any ways to do this? 有什么办法吗?

You could combine find with grep and it's -v option. 您可以将find与grep结合使用,它是-v选项。 find . -type d | grep -v "[foobar]"

find supports negation by means of ! find支持否定的方法! and -not . -not The latter is not POSIX-compliant. 后者不符合POSIX。 In order to use ! 为了使用! you have to precede it with backslash or put it inside single quotes. 您必须在其前面加上反斜杠或将其放在单引号中。

A simple regular glob will work in this particular example: 一个简单的常规glob将在此特定示例中起作用:

$ ls
[a]b [ab]
$ echo \[*\]
[ab]

For more complex patterns you can enable extglob : 对于更复杂的模式,可以启用extglob

!(pattern-list)
     Matches anything except one of the given patterns

( and similar globs ) 和类似的问题

Or using find: 或使用查找:

find dir ! -name ...
find -type d -name '*\]?*'

除非您坚持要打开支架检查...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM