Search content of files in Linux Shell

,

So by creating an executable file in /usr/bin will allow you to search recursively in a directory. I used it by creating a file called sfiles and when run it asks you for the content you are looking for.

# Put in /usr/bin as an executable to allow use by any user
SEARCHTERM=""

if [ $# -eq 0 ]
then
        echo "Please enter a search term or phrase: "
        read SEARCHTERM
else
        SEARCHTERM=$@
fi

while [ "$SEARCHTERM" = "" ]
do
        echo "Please enter a search term or phrase: "
        read SEARCHTERM
done

echo "Searching for '$SEARCHTERM'..."

find ./ -type f -exec grep -l ''"$SEARCHTERM"'' {} ;

 

Skills

Posted on

April 12, 2015

Submit a Comment

Your email address will not be published. Required fields are marked *