my code till now is. I am a beginner.
count=0
for f in $ls do
count+= grep -c "classes" $f
done
echo $count
To include all subdirectories in a search, add the -r operator to the grep command. This command prints the matches for all files in the current directory, subdirectories, and the exact path with the filename.
You can use grep command or find command as follows to search all files for a string or words recursively.
To recursively search for a pattern, invoke grep with the -r option (or --recursive ). When this option is used grep will search through all files in the specified directory, skipping the symlinks that are encountered recursively.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With