I have to find some words in a lot of log files after I've run a failed build.
- 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.
- 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.
- 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.
grep -rn "myword" **/*.log
orgrep -n "myword" *.log
suggested in this post. – Marc Le Bihan Mar 31 '21 at 12:14find
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 sureshopt globstar
says "on" (turn it on withshopt -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:27find
, 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:44grep -r --include='*.log' myword
– steeldriver Mar 31 '21 at 13:50find . -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