fif 965 B

1234567891011121314151617181920212223242526272829303132333435
  1. #!/bin/bash
  2. # fif - Find In Files
  3. # this script lists the filenames of all files under <DIR> that contain <TERM>
  4. readonly DIR=$1
  5. readonly TERM=$2
  6. readonly USER_FILTER=$3
  7. readonly DEFAULT_FILTER="~$|\.kate-swp$|\.o$|\.git/|\.Po$|node_modules/"
  8. [ "$USER_FILTER" ] && FILTER="$USER_FILTER|$DEFAULT_FILTER" || FILTER="$DEFAULT_FILTER"
  9. read -r -d '' USAGE <<-USAGE_MSG
  10. fif - Find In Files
  11. USAGE:
  12. fif <DIR> <TERM> [FILTER]
  13. EXAMPLES:
  14. fif /home/user search-term
  15. fif . "consecutive words" .bak
  16. fif ./src "nonAlphaNumeric(" "\.c$|\.db$"
  17. DESCRIPTION:
  18. Search <DIR> recursively for all files containing <TERM>
  19. Returns names of files containing <TERM> (excluding <FILTER> regex match)
  20. <TERM> and <FILTER> must be quoted in all but the simplest cases
  21. Default filters are: ${DEFAULT_FILTER}
  22. USAGE_MSG
  23. set -e
  24. case $TERM in
  25. '' ) printf "$USAGE\n" ;;
  26. * ) find $DIR -print0 | xargs --null grep -ls "$TERM" | grep -v -E "$FILTER" ;;
  27. esac