find . -name '*.log' -mtime -2 -exec grep -Hc Exception {} \; | grep -v :0$
The find
:
-name '*.log'
-- match files ending with .log
-mtime -2
-- match files modified within the last 2 days-exec CMD ARGS \;
-- for each file found, execute command, where {}
in ARGS
will be replaced with the file's pathThe grep
:
-c
is to print the count of the matches instead of the matches themselves-H
is to print the name of the file, as grep
normally won't print it when there is only one filename argumentpath:count
. Files that didn't match "Exception" will still be printed, with 0 as countgrep
filters the output of the first, excluding lines that end with :0
(= the files that didn't contain matches)Extra tips:
-i
for grep
to make the search case insensitivefind
match strictly only files, add -type f
| mailx -s 'error counts' yourmail@example.com
The -H
flag of grep
may not work in older operating systems, for example older Solaris. In that case use ggrep
(GNU grep) instead, if it exists.