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?

1

2 Answers 2

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
4
  • This is probably the more generic answer, since systems always have find installed. Note that parallel does offer faster processing since find operates serially. Feb 13, 2011 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, 2011 at 18:49
  • I think parallel allows a more natural syntax than xargs, and it's got some very fun features. Feb 13, 2011 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, 2011 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.

3
  • 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, 2011 at 21:57
  • -j+0 is default from version 20110122 thus can be left out.
    – Ole Tange
    Feb 14, 2011 at 14:55
  • Oh yeah forgot about that. Thanks Ole. The version in macports I'm using is quite a bit older. Feb 14, 2011 at 15:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .