Find text string in file

I save a script named “findtext” that will look inside any file smaller than my maximum (avoiding searching huge binary file)


#!/bin/bash

maxFileSize=10M #don't search inside huge binary files

if [ $# -eq 1 ]; then
srchText=$1
fDir="."
fntmp="*"
elif [ $# -eq 2 ]; then
fDir=$1
srchText=$2
fntmp="*"
elif [ $# -eq 3 ]; then
fDir=$1
srchText=$2
fntmp=$3
else
echo usage: "findtext [DIR] TEXT [FILENAME]"
echo searches for TEXT under DIR and echos back filenames
exit
fi

# dont use -execdir, or you wont see which directory the found files are in!
find $fDir -type f -name "$fntmp" -size -$maxFileSize -exec grep --ignore-case --files-with-matches "$srchText" {} +

Post a Comment

Your email address is never shared. Required fields are marked *