How do I recursively
grep all directories and subdirectories?
find . | xargs grep "texthere" *
grep -r "texthere" .
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
If you know the extension or pattern of the file you would like, another method is to use
grep -r --include "*.txt" texthere .
You can also mention files to exclude with
If you frequently search through code, Ag (The Silver Searcher) is a much faster alternative to grep, that’s customized for searching code. For instance, it’s recursive by default and automatically ignores files and directories listed in
.gitignore, so you don’t have to keep passing the same cumbersome exclude options to grep or find.
I now always use (even on Windows with GoW — Gnu on Windows):
grep --include="*.xxx" -nRHI "my Text to grep" *
That includes the following options:
Recurse in directories only searching file matching
Prefix each line of output with the line number within its input file.
-R, -r, --recursive
Read all files under each directory, recursively; this is equivalent to the
Print the filename for each match.
Process a binary file as if it did not contain matching data;
this is equivalent to the
And I can add ‘
-nRHIi), if I want case-insensitive results.
I can get:
/home/vonc/gitpoc/passenger/gitlist/github #grep --include="*.php" -nRHI "hidden" * src/GitList/Application.php:43: 'git.hidden' => $config->get('git', 'hidden') ? $config->get('git', 'hidden') : array(), src/GitList/Provider/GitServiceProvider.php:21: $options['hidden'] = $app['git.hidden']; tests/InterfaceTest.php:32: $options['hidden'] = array(self::$tmpdir . '/hiddenrepo'); vendor/klaussilveira/gitter/lib/Gitter/Client.php:20: protected $hidden; vendor/klaussilveira/gitter/lib/Gitter/Client.php:170: * Get hidden repository list vendor/klaussilveira/gitter/lib/Gitter/Client.php:176: return $this->hidden; ...