Differences between revisions 7 and 12 (spanning 5 versions)
Revision 7 as of 2008-11-22 14:08:37
Size: 1588
Editor: localhost
Comment: converted to 1.6 markup
Revision 12 as of 2011-05-26 13:31:47
Size: 1677
Editor: sbl-eh4-rp1
Comment:
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
Line 8: Line 7:
grep -r "$search" . grep -r -- "$search" .
Line 11: Line 10:
grep -r -l "$search" . grep -r -l -- "$search" .
Line 13: Line 12:
You can use [[UsingFind|find]] if your {{{grep}}} lacks a {{{-r}}} option, or if you want to avoid traversing symbolic links:
Line 14: Line 14:
You can use [[UsingFind|find]] if your {{{grep}}} lacks a {{{-r}}} option:
Line 16: Line 15:
find . -type f -exec grep -l "$search" {} \; find . -type f -exec grep -l -- "$search" {} \;
Line 18: Line 17:
Line 22: Line 20:
Line 23: Line 22:
find . -type f -exec grep -l "$search" {} + find . -type f -exec grep -l -- "$search" {} +
Line 25: Line 24:
Line 29: Line 27:
Line 30: Line 29:
find . -type f | xargs grep -l "$seach" find . -type f | xargs grep -l -- "$search"
Line 32: Line 31:
However, if your filenames contain spaces or other metacharacters, you'll need to use the BSD/GNU {{{-print0}}} option:
Line 33: Line 33:
However, if your filenames contain spaces or other metacharacters, you'll need to use the BSD/GNU {{{-print0}}} option:
Line 35: Line 34:
find . -type f -print0 | xargs -0 grep -l "$search" find . -type f -print0 | xargs -0 grep -l -- "$search"
Line 37: Line 36:
The {{{-print0}}} / {{{-0}}} options ensure that any file name can be processed, even one containing [[BashFAQ/020|blanks, TAB characters, or newlines]].
Line 38: Line 38:
The {{{-print0}}} / {{{-0}}} options ensure that any file name can be processed, even one containing [[BashFAQ/020|blanks, TAB characters, or newlines]]. ----
CategoryShell

How can I recursively search all files for a string?

90% of the time, all you need is one of these:

# Recurse and print matching lines (GNU grep):
grep -r -- "$search" .

# Recurse and print only the filenames (GNU grep):
grep -r -l -- "$search" .

You can use find if your grep lacks a -r option, or if you want to avoid traversing symbolic links:

find . -type f -exec grep -l -- "$search" {} \;

The {} characters will be replaced with the current file name.

This command is slower than it needs to be, because find will call grep with only one file name, resulting in many grep invocations (one per file). Since grep accepts multiple file names on the command line, find can be instructed to call it with several file names at once:

find . -type f -exec grep -l -- "$search" {} +

The trailing '+' character instructs find to call grep with as many file names as possible, saving processes and resulting in faster execution. This example works for POSIX find, e.g. with Solaris, as well as very recent GNU find.

Traditional Unix has a helper program called xargs for the same purpose:

find . -type f | xargs grep -l -- "$search"

However, if your filenames contain spaces or other metacharacters, you'll need to use the BSD/GNU -print0 option:

find . -type f -print0 | xargs -0 grep -l -- "$search"

The -print0 / -0 options ensure that any file name can be processed, even one containing blanks, TAB characters, or newlines.


CategoryShell

BashFAQ/008 (last edited 2015-03-05 00:24:46 by izabera)