How to find all files within folder containing specific text (string) if text found return 1 if not return 0 in linux?
grep -r "34161FA8203289722240CD40" /usr/lib/cgi-bin/ParkingSoft/api/v3/LaneApi/ETC/MywebSocket /*.txt
CodePudding user response:
Try This:
grep -rwl 'PATH/targetFolder/' -e 'target_string' | awk -F "/" '{print $NF}'
The above command returns the name of all files that contains the target_string
.
To know about -rwl
check this answer, However awk -F "/" '{print $NF}'
just split the grep
output and return the last part. (file name in your case)
CodePudding user response:
The -q
option returns (exit code) 1 when no match is found. Try:
echo "string" | grep -q in && echo yes
echo "string" | grep -q out && echo yes
In your case:
searchdir="/usr/lib/cgi-bin/ParkingSoft/api/v3/LaneApi/ETC/MywebSocket "
if [ ! -d "$searchdir" ]; then
echo "Check searchdir. Is 'ETC' really in uppercase and is `Mywebsocket ` including a space?"
else
if grep -rwq '34161FA8203289722240CD40' "${searchdir}/*.txt; then
echo "String found in one of the files."
fi
fi