The first parameter represents the regular expression to search for, while the second one represents the directory that should be searched. In this case, . means the current directory.
Note: This works for GNU grep, and on some platforms like Solaris you must specifically use GNU grep as opposed to legacy implementation. For Solaris this is the ggrep command.
Note that find . -type f | xargs grep whatever sorts of solutions will run into "Argument list to long" errors when there are too many files matched by find.
The best bet is grep -r but if that isn't available, use find . -type f -exec grep -H whatever {} \; instead.
For larger projects, the quickest grepping tool is ripgrep which greps files recursively by default:
rg "pattern" .
It's built on top of Rust's regex engine which uses finite automata, SIMD and aggressive literal optimizations to make searching very fast. Check the detailed analysis here.
The first parameter represents the regular expression to search for, while the second one represents the directory that should be searched. In this case,
.
means the current directory.Note: This works for GNU grep, and on some platforms like Solaris you must specifically use GNU grep as opposed to legacy implementation. For Solaris this is the
ggrep
command.Note that
find . -type f | xargs grep whatever
sorts of solutions will run into "Argument list to long" errors when there are too many files matched by find.The best bet is
grep -r
but if that isn't available, usefind . -type f -exec grep -H whatever {} \;
instead.Just for fun, a quick and dirty search of *.txt files if the @christangrant answer is too much to type :-)
grep -r texthere .|grep .txt
grep -r "texthere" .
(notice period at the end)(^credit: https://stackoverflow.com/a/1987928/1438029)
Clarification:
grep -r "texthere" /
(recursively grep all directories and subdirectories)grep -r "texthere" .
(recursively grep these directories and subdirectories)grep recursive
grep help
$ grep --help
Alternatives
ack
(http://beyondgrep.com/)ag
(http://github.com/ggreer/the_silver_searcher)globbing
**
Using
grep -r
works, but it may overkill, especially in large folders.For more practical usage, here is the syntax which uses globbing syntax (
**
):which greps only specific files with pattern selected pattern. It works for supported shells such as Bash +4 or zsh.
To activate this feature, run:
shopt -s globstar
.See also: How do I find all files containing specific text on Linux?
git grep
For projects under Git version control, use:
which is much quicker.
ripgrep
For larger projects, the quickest grepping tool is
ripgrep
which greps files recursively by default:It's built on top of Rust's regex engine which uses finite automata, SIMD and aggressive literal optimizations to make searching very fast. Check the detailed analysis here.