A code-only answer. Explanations available upon request
if [[ -d "$D_path" ]]; then files=( "$D_path"/*abcd* ) num_files=${#files[@]} else num_files=0 fi
I forgot this: by default if there are no files matching the pattern, the files array will contain one entry with the literal string *abcd*. To have the result where the directory exists but no files match => num_files == 0, then we need to set an additional shell option:
shopt -s nullglob
This will result in a pattern that matches no files to expand to nothing. By default a pattern that matches no files will expand to the pattern as a literal string.
$ cat no_such_file cat: no_such_file: No such file or directory $ shopt nullglob nullglob off $ files=( *no_such_file* ); echo "${#files[@]}"; declare -p files 1 declare -a files='([0]="*no_such_file*")' $ shopt -s nullglob $ files=( *no_such_file* ); echo "${#files[@]}"; declare -p files 0 declare -a files='()'