1

Is it possible to use find to only return a result is 3 file names are present in that directory?

of course at the end we can use wc -l to tally them up...

but starting with some sort of a project root directory and then within just those subfolders that match name / regex pattern try to list ones that have within them:

file1.ext1 and file2.ext2 and *.ext3

and then only count the directly with wc -l if all those files are present within it?

2

You can run tests which aren’t built-in to find with -exec or -execdir, and use their result as a find test. In this case:

find . -name \*.ext3 -execdir /usr/bin/test -e file1.ext1 -a -e file2.ext2 \; -printf "%p\n" | sort -u | wc -l

This will look for files matching *.ext3, and for every one it finds, run /usr/bin/test in the containing directory to check whether file1.ext1 and file2.ext2 exist there too. If they do, it outputs the containing directory’s path. The output is then filtered to remove duplicates, and finally counted.

  • Why do you do a sort -u? There shouldn't be more than one file with the same name right? – leeand00 May 3 '18 at 3:09
  • 1
    @leeand00 since we’re matching *.ext3, the same path can be present multiple times in the results: a directory containing n files matching *.ext3, as well as file1.ext1 and file2.ext2, will appear n times. – Stephen Kitt May 3 '18 at 5:17
  • So we're counting the number of directories that match the criteria. Okay. – leeand00 May 3 '18 at 6:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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