Exclude search directories that do not contain a specific file name?

Let's say I have a directory containing several subdirectories, each of which contains several files. I want to check which of these directories do not contain a specific file. For example, if these are my directories:

dir/A:
    foo
    bar
dir/B:
    bar
dir/C:
    foo
dir/D:
dir/E:
    foo
    bar

If I wanted to list all directories not containing foo, I would get:

dir/B
dir/D

Can this be done using unix find, or do I need to use some alternative tool?

+5
source share
4 answers

I found a good solution after several studies:

find . -type d \! -exec test -e '{}/foo' \; -print

If you replace foowith any file you are looking for.

, , .

+7
comm -3 <(find -type d) <(find -name foo -printf '%h\n')
  • , foo
  • ,
0

1 - 1 script ( - ). check.sh:

cd "$1" && [[ ! -f "$2" ]] && pwd

2 - script:

chmod +x ./check.sh

3 - , find:

find . -maxdepth 1 -type d -exec ./check.sh '{}' foo \;
0

, :

ls | xargs -I dir ls dir/foo > /dev/null

, ls */foo (, "foo" ), xargs stderr , foo. stdout, , .

0

All Articles