Need to search a directories with lots of sub-directories for a string inside files:
I\'m using:
grep -c -r \"string here\" *
How can I
It works for me (it gets the total number of 'string here' found in each file). However, it does not display the total for ALL files searched. Here is how you can get it:
grep -c -r 'string' file > out && \
awk -F : '{total += $2} END { print "Total:", total }' out
The list will be in out and the total will be sent to STDOUT.
Here is the output on the Python2.5.4 directory tree:
grep -c -r 'import' Python-2.5.4/ > out && \
awk -F : '{total += $2} END { print "Total:", total }' out
Total: 11500
$ head out
Python-2.5.4/Python/import.c:155
Python-2.5.4/Python/thread.o:0
Python-2.5.4/Python/pyarena.c:0
Python-2.5.4/Python/getargs.c:0
Python-2.5.4/Python/thread_solaris.h:0
Python-2.5.4/Python/dup2.c:0
Python-2.5.4/Python/getplatform.c:0
Python-2.5.4/Python/frozenmain.c:0
Python-2.5.4/Python/pyfpe.c:0
Python-2.5.4/Python/getmtime.c:0
If you just want to get lines with occurrences of 'string', change to this:
grep -c -r 'import' Python-2.5.4/ | \
awk -F : '{total += $2; print $1, $2} END { print "Total:", total }'
That will output:
[... snipped]
Python-2.5.4/Lib/dis.py 4
Python-2.5.4/Lib/mhlib.py 10
Python-2.5.4/Lib/decimal.py 8
Python-2.5.4/Lib/new.py 6
Python-2.5.4/Lib/stringold.py 3
Total: 11500
You can change how the files ($1) and the count per file ($2) is printed.