Reputation: 15
I want to check recursively for two specific files say "hem" and "haw" and print the folders containing both the files.
Upvotes: 0
Views: 41
Reputation: 8064
Try this Shellcheck-clean code:
shopt -s globstar
for hempath in ./**/hem ; do
dir=${hempath%/*}
[[ -e $dir/haw ]] && printf '%s\n' "$dir"
done
globstar
and the **
pattern.${hempath%/*}
../**/hem
instead of **/hem
to ensure that all the matched paths start with ./
so it works even if the files are in the current directory (.
).printf
is used instead of echo
to print the directory path.globstar
was introduced in Bash 4.0, and it was dangerous in versions prior to 4.3 because it used to follow symlinks, possibly leading to infinite recursion (and failure) or unwanted duplicates. See When does globstar descend into symlinked directories?.Upvotes: 0
Reputation: 18864
find <top_folder> -name hem -o name haw -print
or
cd <top_folder>
ls **/hem **/haw
Upvotes: 1