I need to write a Bash script (preferably a one-liner) to find all the subdirectories under the current directory which contain any files with a certain file extension, namely .tf. The tricky part is that there may be more than one .tf file in a folder, and I need the folders - not the filenames.
So for example, suppose my directory contents look something like this:
.
├── docs
│ ├── README.md
│ └── diagram.png
├── project
│ └── main.py
├── Makefile
├── terraform
│ ├── environments
│ │ ├── prod
│ │ │ └── main.tf
│ │ └── staging
│ │ └── main.tf
│ └── module
│ ├── ecs.tf
│ ├── rds.tf
│ ├── s3.tf
│ ├── security_group.tf
│ ├── sqs.tf
│ └── variable.tf
├── tests
| └── test_main.py
└── .terraform
└── ignore_me.tf
In this case I would want the command to return terraform/module, terraform/environments/prod, and terraform/environments/staging. I also need a way to filter out certain directories that should never be included. In the example above note that I did not include the dot-file directory .terraform.
I already know how to write the following find command, which can find the .tf files themselves:
find . -name '*.tf' -not -path '*.terraform*'
However I'm not sure how to write a command to find the directories which contain those files.