Find only those folders that contain a File with the same name as the Folder

Assuming your files are sensibly named, i.e. no need for -print0 etc. You can do this with GNU find like this:

find . -type f -regextype egrep -regex '.*/([^/]+)/\1\.md$'

Output:

./Apple/Banana/Orange/Orange.md
./Apple/Banana/Papaya/Papaya.md
./Apple/Banana/Banana.md

If you only want the directory name, add a -printf argument:

find . -type f -regextype egrep -regex '.*/([^/]+)/\1\.md$' -printf '%h\n'

Output when run on your updated test data:

GeneratedTest/A/AA
GeneratedTest/A
GeneratedTest/C/CC2
GeneratedTest/C/CC1
GeneratedTest/B

find . -type d -exec sh -c '
    dirpath=$1
    set -- "$dirpath"/*.md
    [ -f "$dirpath/${dirpath##*/}.md" ] && [ "$#" -eq 1 ]' sh {} \; -print

The above would find all directories below the current directory (including the current directory) and would execute a short shell script for each.

The shell code would test whether there's a markdown file with the same name as the directory inside the directory, and whether this is the only *.md name in that directory. If such a file exists and if it's the only *.md name, the inline shell script exits with a zero exit status. Otherwise it exits with a non-zero exit status (signalling failure).

The set -- "$dirpath"/*.md bit will set the positional parameters to the list of pathnames matching the pattern (matches any name with a suffix .md in the directory). We can then use $# later to see how many matches we got from this.

If the shell script exits successfully, -print will print the path to the found directory.

Slightly speedier version that uses fewer invocations of the inline script, but that doesn't let you do more with the found pathnames in find itself (the inline script may be further expanded though):

find . -type d -exec sh -c '
    for dirpath do
        set -- "$dirpath"/*.md
        [ -f "$dirpath/${dirpath##*/}.md" ] &&
        [ "$#" -eq 1 ] &&
        printf "%s\n" "$dirpath"
    done' sh {} +

The same commands but without caring about whether there are other .md files in the directories:

find . -type d -exec sh -c '
    dirpath=$1
    [ -f "$dirpath/${dirpath##*/}.md" ]' sh {} \; -print
find . -type d -exec sh -c '
    for dirpath do
        [ -f "$dirpath/${dirpath##*/}.md" ] &&
        printf "%s\n" "$dirpath"
    done' sh {} +

See also:

  • Understanding the -exec option of `find`

On a GNU system, you could do something like:

find . -name '*.md' -print0 |
  gawk -v RS='\0' -F/ -v OFS=/ '
    {filename = $NF; NF--
     if ($(NF)".md" == filename) include[$0]
     else exclude[$0]
    }
    END {for (i in include) if (!(i in exclude)) print i}'