7

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?

Stefhen
  • 143
  • 2
  • 8

2 Answers2

8

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
user9517
  • 115,471
  • 20
  • 215
  • 297
  • 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. – user9517 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
1

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.

Phil Hollenback
  • 14,947
  • 4
  • 35
  • 52