Let's say I'm searching for all files with .log
extension that contain the text of abc
.
When searching for files with any extension would look like
(Ref.: https://unix.stackexchange.com/a/16140/38353 )
find / -xdev -type f -print0 | xargs -0 grep -H "abc"
How could we modify this so that it will search only for files with .log
extension?
I'll be more than happy if you show a better command.
Best Answer
TL;DR
Add
-iname "*.log"
after/
to yourfind
command. Refer to man page for more infoA more detailed answer
The task at hand is the following:
*.log
grep
per each file to find whether or not it contains a specific string.stdout
The example of how that can be accomplished can be seen bellow:
Essentially there's 3 things at play:
find
does the job of finding files AND callinggrep
per list of filenames in the-exec ...{} \+
structure, where{}
will be substituted with all the filenames found.-iname "*.log"
can provide case-insensetive matching of the filenames-exec . . .{} \+
calls the low-levelexecve
function that will spawngrep -l
with the list of all the files found in front of it ( in the place of{}
).\+
is the option that specifies forexecve
to pack as any files as possible in front ofgrep
(the limit is set byARG_MAX
variable, is specific toexec
, and for Ubuntu is at2097152
as can be shown bygetconf ARG_MAX
command ). Once the limit is reached,exec
will repeat the call togrep
with more files packed as arguments. The\
is necessary to ensure+
is interpreted as argument tofind
and not as another shell command.-l
option or grep shows files with matched string.-L
would match files without the string.