Skip to content

Instantly share code, notes, and snippets.

@joest67
Last active December 22, 2015 08:39
Show Gist options
  • Save joest67/6446923 to your computer and use it in GitHub Desktop.
Save joest67/6446923 to your computer and use it in GitHub Desktop.
This shell script help to find matched content in files when you give a keyword.
#!/bin/bash
# Program:
# this program help us to find something in files'content.
# History:
# 05/11/2013 Joest First release
#
# usage:
# copye it to /usr/bin/ folder
PATH=/bin:/sbin:/usr/bin:/usr/sbin
export PATH
# can use absolute path or relative patten
read -p "enter the filepath where you want to find: " filepath
# no need to add ""
read -p "enter your patten: " patten
echo "patten: $patten"
# find files in filepath recursively
files=$(find $filepath)
i=0
for f_ in $files
do
# find result then result is 1
temp=$(grep --color=auto "$patten" $f_) && result=0 || result=1
if [ $result -eq 0 ]; then
i=$(($i + 1))
echo "Filename: $f_"
echo "Patten: $temp"
fi
done
echo "$i results."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment