pattern="foo"
for _dir in *"${pattern}"*; do
[ -d "${_dir}" ] && dir="${_dir}" && break
done
echo "${dir}"
This is better than the other shell solution provided because
- it will be faster for huge directories as the pattern is part of the glob and not checked inside the loop
- actually works as expected when there is no directory matching your pattern (then
${dir}
will be empty)
- it will work in any POSIX-compliant shell since it does not rely on the
=~
operator (if you need this depends on your pattern)
- it will work for directories containing newlines in their name (vs.
find
)