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've seen similar questions to this, but nothing I can find related directly to files.

Is there any way to give me a list of directories which do not contain a specific file?

Example:

/dir1/good
/dir1/stuff
/dir2/junk
/dir2/morejunk
/dir3/good
/dir4/junk

I'd like to run a find command where I would only return the directory names which do not contain the file 'good' (dir2 and dir4). Is there any way with find or some combo thereof to do this? The only way I can do this now is to run a find for the file 'good', save that output and use some awk, sort and comm -3 commands to get a diff for what I need.

Is there any easier way to do this?

share|improve this question

2 Answers 2

up vote 6 down vote accepted

You can use -exec test ... to check for the existence of the file good in a directory e.g.

find /path/to/search -type d \! -exec test -e '{}/good' \; -print
share|improve this answer
    
This is probably the more generic answer, since systems always have find installed. Note that parallel does offer faster processing since find operates serially. –  Phil Hollenback Feb 13 '11 at 18:41
    
I've seen you use parallel a few times, I must put some time aside to have a play with it. –  Iain Feb 13 '11 at 18:49
    
I think parallel allows a more natural syntax than xargs, and it's got some very fun features. –  Phil Hollenback Feb 13 '11 at 18:53
    
Thanks Iaian, this is exactly what I needed. Just couldn't seem to get the find syntax down. Will remember this for sure. –  Stefhen Feb 13 '11 at 21:53

You can do this easily with find and GNU parallel:

$ file=foo && find . -type d | parallel -j+0 "[ -f {}/$file ] || echo $file not in {}"

That command works by feeding every directory under the current directory into parallel. Parallel then launches parallel jobs to check for file foo in every directory, and prints a message if the file does not exist in the directory.

share|improve this answer
    
Thanks for the parallel example, I will check this out. I haven't explored using this app much yet, but have heard good things about it. –  Stefhen Feb 13 '11 at 21:57
    
-j+0 is default from version 20110122 thus can be left out. –  Ole Tange Feb 14 '11 at 14:55
    
Oh yeah forgot about that. Thanks Ole. The version in macports I'm using is quite a bit older. –  Phil Hollenback Feb 14 '11 at 15:29

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.