Normally while searching using grep, the command I was using was this:
grep -nri "String"
whereas most of my colleagues do this:
grep -nri "String" *
What does the latter do (the *
part)?
command linegrep
Normally while searching using grep, the command I was using was this:
grep -nri "String"
whereas most of my colleagues do this:
grep -nri "String" *
What does the latter do (the *
part)?
Best Answer
grep
with the-r
flag operates on all files in the specified directories recursively:By default, if no directories are given, then
grep
will process all files in the current directory.In
grep -r ... *
, then, the shell expands*
to all files and directories in the current directory (usually except those that begin with a.
), andgrep
then works recursively on them.So, if you had a directory that contained, for example:
where the names ending with
/
are directories, thengrep -r
would also process the.gitignore
file and everything in.git
, butgrep -r ... *
would exand togrep -r ... foo bar
, and would end up excluding.gitignore
and.git
(but it would includefoo/.foo2
).Also note the point about symbolic links - if one of the files in the expansion of
*
was a symlink, the symlink target would be processed if you used*
. So with*
,/foo/bar
will be processed as the target oflink1
, but not/foo2/bar2
as the target oflink2
.The overall effect:
Which you want to do, of course, depends on whether you want those files and directories included in the search; but I tend to prefer having
grep
itself do the excluding and including using the--exclude
/--include
and other options.