1

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?

1
  • To clarify - you are looking for a regex that matches a string that has text between // characters that is not between [] characters? Would that mean any string that doesn't match /[.*]/ - with appropriate escaping of the / and [] characters, of course... ? As for "match something that is not"... see stackoverflow.com/questions/406230/… Commented Mar 6, 2013 at 17:14

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

Probably the most simple solution.
This is incorrect. You need to switch to grep -Fv to switch to literal matching, or escape the regex special \[ because the current regular expression will match any string which contains either f or o or b or a or r anywhere.
this is not ideal because does not stop find from 'finding' the files that do not match. This is a major setback when you are deal with thousands+ files and directories that you want to exclude.
3

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 ... 

2 Comments

Works in general, but piping find to grep seems easier.
As long as you're not going to use it beyond simply looking, that will be fine, though I think the glob is easier (and safer if there are any spaces in file names).
2

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.

Comments

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

Unless you insist on opening bracket check...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.