Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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:

/home/USER/logfilesError/xy_2071.log
/home/USER/logfilesError/xy_2072.log
/home/USER/logfilesError/xy_2073.log

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):

/home/USER/logfilesError/xy_55.log
/home/USER/logfilesError/xy_1015.log

Question:

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)*
share|improve this question

1 Answer 1

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.
share|improve this answer
    
Or if your grep is not the GNU grep: -exec grep -l ERROR /dev/null {} + –  Stéphane Chazelas Jun 6 at 8:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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