Skip to content

Instantly share code, notes, and snippets.

@jayers99
Last active March 27, 2019 19:49
Show Gist options
  • Save jayers99/bb2bf37b029d7e3cbf972bbbf1fe2194 to your computer and use it in GitHub Desktop.
Save jayers99/bb2bf37b029d7e3cbf972bbbf1fe2194 to your computer and use it in GitHub Desktop.
get all the directory names that contain some string
#!/bin/bash
# return a list of directories the contain given text
# example: ~/scripts/tfgrep_dirs.sh '^\s*source\s*=\s*.*some_module_name'
file_paths=$(grep -irlP --include \*.tf --exclude-dir=.terraform --exclude-dir=.git $@)
dirs_array=()
for i in $file_paths; do
dirs_array+=($(dirname $i))
done
dirs_uniq=($(echo "${dirs_array[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
for i in "${dirs_uniq[@]}"; do
echo $i
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment