Reputation: 53
Folder_name = "D:\newfolder\xxx"
echo "enter keyword"
read string
if grep $string $Folder_name;
then
echo "yes"
else
echi "no"
fi
Upvotes: 1
Views: 166
Reputation: 410
You can use this to search
cd "$Folder_name" && count=$(grep -R -c "$string")
if [ $count -gt 0 ]; then
echo "Yes"
else
echo "No"
fi
This would recursively search for all files in the folder.
Upvotes: 1
Reputation: 107090
Are you looking for a file that contains a particular string, or are you looking for a file name that contains a particular string?
find . -type f -exec grep -l "string" {} \;
find . -type f | grep "string"
Upvotes: 1
Reputation: 247250
I would say
found=false
for file in *; do
if grep -q "$string" "$file"; then
found=true
break
fi
done
if $found; then
echo "at least one file contains $string"
else
echo "no files contain $string"
fi
Upvotes: 1
Reputation: 11
If you are looking for a Yes/No response, you can use this one line command:
grep -q $string $Folder_name/* && echo 'Yes'|| echo 'No'
Upvotes: 1
Reputation: 782785
Use this:
Folder_name=/newfolder/xxx
echo "enter keyword"
read string
if grep -q -F "$string" "$Folder_name"/*
then echo yes
else echo no
fi
=
in shell variable assignments.grep
requires filename arguments, not directories, unless you use the -r
option to search the directory recursively.-q
option tells grep
not to print the matching lines.-F
option tells it to treat $string
as a verbatim string rather than a regular expression.Upvotes: 1