Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 4 down vote accepted

You can find command like:

$ find . -regex ".*folder\(A\|B\)" -type d '!' -exec test -d '{}/search' ';' -print
share|improve this answer
    
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 –  DTest 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. –  DTest 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 –  DTest Dec 15 '10 at 18:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.