Posted: 5 years ago

Filed under: Sysadmin

Tagged with:

Follow comments

Shell script: find files with a filename containing keywords

I just wanted to save a little shell script I wrote that searches a directory for files with a certain filename that contain a certain text string.

if [ $# -ne 3 ]
then
        echo "Usuage: fancyFind.sh   
        exit
fi

directory=$1
filename=$2
keywords=$3

for file in $( find $directory -name $filename )
do
        matches=`cat $file | grep "$keywords"`
        if [ -n "$matches" ]
        then
               echo "$file"
        fi
done

Leave a Reply