0

Hello I´m currently trying to search for logfiles which are starting with xy_Number and are created/edited today(not last 24 hrs).

I tried:

find /home/USER/logfilesError/ -maxdepth 1 -type f -daystart -mtime -1 print0 | xargs -0 grep - l xy_*

Output:

find: paths must precede expression: print0

I need to combine:

find -maxdepth 1 -type f -daystart -mtime -1

and

ls | grep -E "xy_"

What I want to achieve is:

Search logfiles from a specific location which today's date and then go a step further and search those files and output the ones which have lines starting with ERROR. (other lines INFO and DEBUG)


So the search works fine and I want to go further with checking the files after the search with a grep to find the files containing ERROR

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

How do I combine those in a script? With If? Can someone help me with this?

  • Your -print0 is missing a hyphen and thus interpreted as a path name. This causes the error message. – Dubu Jun 6 '14 at 8:55
1

Why use grep, find can do the job:

find /home/USER/logfilesError/ -maxdepth 1 -type f -name "xy_*" -daystart -mtime -1
  • Thanks! I dont know why I didnt tought about this. It works as expected. – BlueFox Jun 6 '14 at 5:49
1

By the sounds of it, you're looking for -name:

find /path -maxdepth 1 -type f -name 'xy_*' -daystart -mtime -1 -exec grep -H ERROR {} +

From man find:

-name pattern

Base of file name (the path with the leading directories removed) matches shell pattern pattern. The metacharacters (‘*’, ‘?’, and ‘[]’) match a ‘.’ at the start of the base name (this is a change in findutils-4.2.2; see section STANDARDS CONFORMANCE below). To ignore a directory and the files under it, use −prune; see an example in the description of −path. Braces are not recognised as being special, despite the fact that some shells including Bash imbue braces with a special meaning in shell patterns. The filename matching is performed with the use of the fnmatch(3) library function. Don’t forget to enclose the pattern in quotes in order to protect it from expansion by the shell.

  • Thanks! This works aswell but I receive a warning: you have specified the -maxdepth option after a non-option argument -name, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments. – BlueFox Jun 6 '14 at 5:48
  • 1
    That's why -name is before -maxdepth, just switch them and it will work too. – chaos Jun 6 '14 at 5:50
  • 1
    @BlueFox GNU find doesn't like it when tests are before options. I've moved it, anyway. :-) – Chris Down Jun 6 '14 at 6:18

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.