You can use grep
tool to search recursively the current folder, like:
grep -r "class foo" .
Note: -r
- Recursively search subdirectories.
You can also use globbing syntax to search within specific files such as:
grep "class foo" **/*.c
Note: By using globbing option (**
), it scans all the files recursively with specific extension or pattern. To enable this syntax, run: shopt -s globstar
. You may also use **/*.*
for all files (excluding hidden and without extension) or any other pattern.
If you've the error that your argument is too long, consider narrowing down your search, or use find
syntax instead such as:
find . -name "*.php" -execdir grep -nH --color=auto foo {} ';'
Alternatively, use ripgrep
.
If you're working on larger projects or big files, you should use ripgrep
instead, like:
rg "class foo" .
Checkout the docs, installation steps or source code on the GitHub project page.
It's much quicker than any other tool like GNU/BSD grep
, ucg
, ag
, sift
, ack
, pt
or similar, since it is built on top of Rust's regex engine which uses finite automata, SIMD and aggressive literal optimizations to make searching very fast.
It supports ignore patterns specified in .gitignore
files, so a single file path can be matched against multiple glob patterns simultaneously.
You can use common parameters such as:
-i
- Insensitive searching.
-I
- Ignore the binary files.
-w
- Search for the whole words (in the opposite of partial word matching).
-n
- Show the line of your match.
-C
/--context
(e.g. -C5
) - Increases context, so you see the surrounding code.
--color=auto
- Mark up the matching text.
-H
- Displays filename where the text is found.
-c
- Displays count of matching lines. Can be combined with -H
.
.
as a single-character wildcard, among others. My advice is to alway use either fgrep or egrep. – Walter Tross-H
with-l
(and maybegrep
withfgrep
). To exclude files with certain patterns of names you would usefind
in a more advanced way. It's worthwile to learn to usefind
, though. Justman find
. – Walter Trossfind … -exec <cmd> +
is easier to type and faster thanfind … -exec <cmd> \;
. It works only if<cmd>
accepts any number of file name arguments. The saving in execution time is especially big if<cmd>
is slow to start like Python or Ruby scripts. – hagellogrep "pattern" path/*.txt
– fedorqui 'SO stop harming'