Hello I want to output ERROR messages from logfiles from the current date.

First I search for the logs of today with specific prefix:

find /home/USER/logfilesError/ -maxdepth 1 -type f -name "xy_*" -daystart -mtime -1

It gives me that output:


In these files I want to search for the string "ERROR":

grep -rl "ERROR" /home/USER/logfilesError/

This gives me all the Logfiles containing "ERROR" not only from today.

Output(only some of the output):



How can I combine those in a script?

The Syntax from a line in the logfiles are:

2013-11-24 06:30:30,549 [main] ERROR *(+Errormessage)*

Try this:

find /home/USER/logfilesError/ -maxdepth 1 -type f -name "xy_*" \
    -daystart -mtime -1 -exec grep -Hl "ERROR" "{}" +

From man find:

-exec command {} +
              This  variant  of the -exec action runs the specified command on
              the selected files, but the command line is built  by  appending
              each  selected file name at the end; the total number of invoca‐
              tions of the command will  be  much  less  than  the  number  of
              matched  files.   The command line is built in much the same way
              that xargs builds its command lines.  Only one instance of  `{}'
              is  allowed  within the command.  The command is executed in the
              starting directory.
  • Or if your grep is not the GNU grep: -exec grep -l ERROR /dev/null {} + Jun 6 '14 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.