0

I have to find some words in a lot of log files after I've run a failed build.

  1. If I do that:
grep -r "myword"

I have too much responses. I don't want to search all the files in all the sub directories.

  1. If I do that:
grep -r "myword" *.xml

It works because I have an *.xml file in current directory, but I have no responses because... it's not that kind of file I'm searching for.

  1. If I do that:
grep -r "myword" *.log

It responds:
No file or directory of that type

because there's no log file in the current directory.
But there are plenty of them, below, in target subdirectories. (a find . -name *.log will list me them).

How shall I write the grep command to achieve the result I want? I've tried to end my command with a variety of *.log, .log, **/*.log, */**/*.log, but without success.

Marc Le Bihan
  • 1,353
  • 1
  • 11
  • 24
  • Relating: https://unix.stackexchange.com/q/14498/315749 – fra-san Mar 31 '21 at 12:07
  • @fra-san I had no success with `grep -rn "myword" **/*.log` or `grep -n "myword" *.log` suggested in this post. – Marc Le Bihan Mar 31 '21 at 12:14
  • The `find` alternative in the linked Q/A is the portable one (i.e. works in any POSIX shell). Those based on `**` depend on the shell you are using. E.g. if it's Bash, check your version and make sure `shopt globstar` says "on" (turn it on with `shopt -s globstar`); with zsh it should work by default. If your question cannot be considered a duplicate of the Q/A I linked to, please add the shell you are using to it. – fra-san Mar 31 '21 at 12:27
  • @fcbsd I'm unable to remind a so long command. – Marc Le Bihan Mar 31 '21 at 13:31
  • 1
    This won't help you immediately, but the trick is not to remember this long command. The trick is to get familiar with `find`, so you can craft such command *anew* whenever you need and tailor it to your requirements. BTW the exact command posted by @fcbsd cannot work, it uses `()` where `{}` should be. – Kamil Maciorowski Mar 31 '21 at 13:44
  • 1
    With GNU grep, you should be able to use `grep -r --include='*.log' myword` – steeldriver Mar 31 '21 at 13:50
  • @steeldriver Yes, this one works. Thanks! – Marc Le Bihan Mar 31 '21 at 15:18
  • fixed verison: `find . -name "*.log" -type f -exec grep "myword" {} \; -print` when I need to use grep in multiple directories – fcbsd 2 hours ago Delete – fcbsd Mar 31 '21 at 15:24

0 Answers0