How to implement grep in bash scripting? [duplicate]
问题 This question already has answers here : How to perform grep operation on all files in a directory? (5 answers) Closed 18 days ago . I am working on to search a string on files using grep in a directory(in for loop) for file in .* *; do if [[ -f "$file" && `grep -r "$pattern" "$file"` ]]; then path=`pwd`/"$file" echo "$path" fi done 回答1: Avoid the for loop and use something like grep -l "${pattern}" ${PWD}/.* ${PWD}/* or better find ${PWD} -type f -exec grep -l "${pattern}" {} + 回答2: Use find