MechMK1
MechMK1

Reputation: 3378

regex - find files that don't match

I have a list of directories where most are in a format like ./[foobar]/. However, some are formatted like ./[foo] bar/.

I would like to use find (or some other utility my shell offers) to find those directories not matching the first pattern (i.e. having text outside the square braces). Until now, I was unable to find a way to "inverse" my pattern.

Any ways to do this?

Upvotes: 1

Views: 5498

Answers (4)

Wrikken
Wrikken

Reputation: 70460

find -type d -name '*\]?*'

Unless you insist on opening bracket check...

Upvotes: 1

Peter O'Callaghan
Peter O'Callaghan

Reputation: 6186

You could combine find with grep and it's -v option. find . -type d | grep -v "[foobar]"

Upvotes: 3

Kevin
Kevin

Reputation: 56059

A simple regular glob will work in this particular example:

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

For more complex patterns you can enable extglob:

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

(and similar globs)

Or using find:

find dir ! -name ...

Upvotes: 3

Michał Trybus
Michał Trybus

Reputation: 11784

find supports negation by means of ! and -not. The latter is not POSIX-compliant. In order to use ! you have to precede it with backslash or put it inside single quotes.

Upvotes: 2

Related Questions