my code till now is. I am a beginner.
count=0
for f in $ls do
count+= grep -c "classes" $f
done
echo $count
my code till now is. I am a beginner.
count=0
for f in $ls do
count+= grep -c "classes" $f
done
echo $count
Just use
grep -R <stringToSearch> <dirName>
e.g to search "text" in current directory and all the files inside
grep -R "text" .
If you want to get number of occurrences use wc -l
as pipe
grep -R "text" . | wc -l
grep -rnw <Directory_path> -e "Pattern"
Example, if you want to find "helloWorld" in all the files of the directory ~/abc/xyz
, then run the following command-
grep -rnw ~/abc/xyz -e "helloWorld"
To search a string "HelloWorld" in the current directory, use the following command-
grep -rnw . -e "helloWorld"
I believe find will be the BEST option for this requirement, could you please try following and let me know if this helps you.
find . -type f -exec grep -i "test" {} + | wc -l
OR
find . -type f | xargs grep -i "test" | wc -l
You could mention your path in place of DOT above too.
count=0
for f in *
do
if [ -f "$f" ] #check if that file is regular file
then
count=$(($count+$(grep -c "classes" "$f")))
fi
done
echo $count