3

I need some help constructing a directory listing using 'find'?

An example directory structure looks something like this:

/ (root)
 - /foo
   - /folderA
   - /folderB
 - /bar
   - /folderA
     -/search
   - /folderB

What I want to find is a list of 'folderA' or 'folderB' directories that do NOT have a 'search' folder. Requested output would be:

/foo/folderA
/foo/folderB
/bar/folderB

I am assuming this can be accomplished with 'find' on a *nix system, but am pretty green with the command. All help is appreciated.

SOLVED: Thank you Khaled for your response leading me in the right direction. Needed a slight modification to include the -E option, but the final solution looked like this:

find -E . -regex '.*(folderA|folderB)' -type d '!' -exec test -d '{}/search' ';' -print

Ben Pilbrow
  • 12,041
  • 5
  • 36
  • 57
Derek Downey
  • 3,955
  • 4
  • 27
  • 29

1 Answers1

4

You can find command like:

$ find . -regex ".*folder\(A\|B\)" -type d '!' -exec test -d '{}/search' ';' -print
Khaled
  • 36,533
  • 8
  • 72
  • 99
  • i was testing this out, but am having an issue with the -regex portion. I need it to match on 'folderA|folderB', not 'folder(A|B)'. When I tried it, I was able to match `find . -regex ".*folderA" -type d -print` by itself, but not `find . -regex ".*\(folderA\|folderB\)" -type d -print` Do I need more than one regex. Word boundaries `\\b` didn't seem to help either – Derek Downey Dec 15 '10 at 17:34
  • I think the issue is you escaping the pipe between `folderA|folderB`. You're already quoting it, so find is interpreting it as a literal `|` character. – jgoldschrafe Dec 15 '10 at 17:59
  • found it. needed the -E option for find. Thanks so much for your help. Will post the final find in another answer for the exact string I used. – Derek Downey Dec 15 '10 at 18:00
  • @jgoldschrafe2 I tried it without the escape and it still didn't work. The answer was the -E option for find: `find . -regex '.*(folderA|folderB)' -type d '!' -exec test -d '{}/search' ';' -print` doesn't work, but `find -E . -regex '.*(folderA|folderB)' -type d '!' -exec test -d '{}/search' ';' -print` does – Derek Downey Dec 15 '10 at 18:04