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
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.Sign up to join this community
grep with the
-r flag operates on all files in the specified directories recursively:
-r, --recursive Read all files under each directory, recursively, following symbolic links only if they are on the command line. Note that if no file operand is given, grep searches the working directory. This is equivalent to the -d recurse option.
By default, if no directories are given, then
grep will process all files in the current directory.
grep -r ... *, then, the shell expands
* to all files and directories in the current directory (usually except those that begin with a
grep then works recursively on them.
So, if you had a directory that contained, for example:
.git/ .gitignore foo/ foo/.foo2 foo/link2 -> /foo2/bar2 bar link1 -> /foo/bar
where the names ending with
/ are directories, then
grep -r would also process the
.gitignore file and everything in
grep -r ... * would exand to
grep -r ... foo bar, and would end up excluding
.git (but it would include
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 of
link1, but not
/foo2/bar2 as the target of
The overall effect:
w/o * with * .git/ + - .gitignore + - foo/ + + foo/.foo2 + + foo/link2 -> /foo2/bar2 - - bar + + link1 -> /foo/bar - +
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
--include and other options.