Search code examples
bashsh

Find and display only multiple name match


I need to use bash to strictly match multiple folders names and display these folders. In that example folders with subfolders.

root_folder
  sub1
  sub2  

root_folder1
  sub1
  sub2
  sub3

root_folder2
  sub1

And i only need to print folder(s) which contains folders sub1 and sub3 so it should be root_folder1.

I tried such approach, it doesn't do what expected. find source_dir -type d \( -iname "sub1" -a -iname "sub3" \)


Solution

  • find can't do this by itself. Use find to find all directories, then pipe this to a loop that checks if it contains sub1 and sub3:

    find source_dir -type d | while read -r dir; do
        if [[ -d "$dir/sub1" && -d "$dir/sub3" ]]
        then printf "%s\n" "$dir"
        fi
    done